poj 1251 Jungle Roads
来源:互联网 发布:eclipse新建教程 java 编辑:程序博客网 时间:2024/06/08 13:45
最小生成树的模板题
Kruskal算法
贪心。。
不过要是地点的名字复杂点,就没那么好做了。。。
#include<cstdio>#include<iostream>#include<queue>#include<algorithm>#include<cstring>#include<string>#include<map>using namespace std;struct sb{ int s; int e; int v; bool operator< (const sb&wakaka)const { return v<wakaka.v; }}way[10010];int city[30];int find(int x){ if(x==city[x]) return x; else return city[x]=find(city[x]);}int main(){ int n; while(cin>>n&&n) { int cnt=0; for(int i=0;i<=n;i++) { city[i]=i; } for(int i=1;i<n;i++) { char whos; cin>>whos; int m; cin>>m; for(int j=1;j<=m;j++) { cnt++; way[cnt].s=whos-'A'; char whoe; cin>>whoe>>way[cnt].v; way[cnt].e=whoe-'A'; } } sort(way+1,way+cnt+1); long long ans=0; for(int i=1;i<=cnt;i++) { int f1=find(way[i].s); int f2=find(way[i].e); if(f1==f2) continue; ans=ans+way[i].v; if(f1<f2) city[f2]=f1; else city[f1]=f2; } cout<<ans<<endl; } return 0;}
0 0
- POJ 1251 Jungle Roads
- poj 1251 Jungle Roads
- poj 1251 Jungle Roads
- POJ-1251 jungle roads
- poj 1251 Jungle Roads
- POJ-1251-Jungle Roads
- POJ 1251 Jungle Roads
- Poj 1251 Jungle Roads
- POJ-1251-Jungle Roads
- POJ 1251 Jungle Roads
- POJ 1251 Jungle Roads
- POJ-1251-Jungle Roads
- POJ 1251 Jungle Roads
- POJ:1251 Jungle Roads
- POJ 1251 Jungle Roads
- POJ 1251 Jungle Roads
- POJ-1251 Jungle Roads
- poj 1251 Jungle Roads
- PCA人脸识别学习及C语言实现
- 51. N-Queens
- 崩溃日志管理工具的重要性
- [CodeForces 279C] Ladder (DP)
- ZOJ Problem Set - 3946 Highway Project
- poj 1251 Jungle Roads
- Java中回调函数编写
- 求指导。。。运行停止工作的原因。
- 一个采用BF算法的宝石迷阵的消除解法
- 电路设计(一)之上拉电阻与下拉电阻的应用
- 广义逆矩阵
- 文件读写函数 -- 字符串读写
- JMockit学习笔记
- Android ContentProvider基础