POJ
来源:互联网 发布:sql注入怎么过安全狗 编辑:程序博客网 时间:2024/06/06 07:49
题意:
给出各个村庄之间路径的距离,同最短的路径走完所有的村庄,就是最小生成树权值的计算,我用了并查集。
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring> using namespace std;struct aa{int st,en,quan;}p[80];int pre[28],vis[28];bool cmp(aa a,aa b){return a.quan<b.quan;}int find(int x) { if (x != pre[x]) { pre[x] = find(pre[x]); //这个回溯时的压缩路径是精华 } return pre[x]; } // return pre[x] == x?x:pre[x] = find(pre[x]); int main(){int n,ss,co,to,sum;char te,fe;while(scanf("%d",&n)&&n){for(int i=0;i<28;i++)pre[i]=i;memset(vis,0,sizeof(vis));to=0;sum=0;for(int i=0;i<n-1;i++){cin>>te>>co;for(int j=0;j<co;j++){p[to].st=te-'A';cin>>fe;p[to].en=fe-'A';cin>>p[to].quan;//vis[te-'a']=vis[fe-'a']=1;to++;}}sort(p,p+to,cmp);for(int i=0;i<to;i++){int x=p[i].st,y=p[i].en;x=find(x);y=find(y);if(x!=y){sum+=p[i].quan;pre[x]=y;}}cout<<sum<<endl;//cout<<to<<endl;}return 0;}
阅读全文
0 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- 百度搜索引擎的工作原理 鏀惰棌鍒帮細 时间:2015-07-10 文章来源:马海祥博客 访问次数:4330 关于百度以及其它搜索引擎的工作原理,其实大家已经讨论过很多,但随着科技的进步、互联网
- 上传图片(图片相对路径)至数据库,并显示
- 2017-8-21
- i2c通信
- java安卓获取本机ip地址
- POJ
- [置顶] Android中蒲公英第三方提示更新
- NW RFC SDK
- 百度最新搜索引擎工作原理
- 最透彻的关于“随机数种子”和“伪随机数”的产生原理
- Excel中对可见数据求和,SUBTOTAL函数完美逆袭!
- WorldWind瓦片调度策略说明
- poj——2299树状数组求逆序数
- Android 通过蒲公英pgyer的接口 Service 实现带进度下载App 通知栏显示 在线更新 自动更新Demo