POJ 1251
来源:互联网 发布:手机桌面软件推荐 编辑:程序博客网 时间:2024/06/02 18:31
#include <iostream>#include <cstdio>#include <algorithm>using namespace std;int n,p[100],k;struct Edge{ int u,v,w;}edge[10000];bool cmp(Edge a,Edge b){ return a.w<b.w;}int find(int x){ return p[x]==x?x:p[x]=find(p[x]);}int kruskal(){ int ans=0,num=0; for(int i=1;i<=n;i++) p[i]=i; sort(edge,edge+k,cmp); for(int i=0;i<k;i++) { int p1=find(edge[i].u); int p2=find(edge[i].v); if(p1!=p2){ ans+=edge[i].w; num++; p[p1]=p2;}if(num==n-1) return ans; }}int main(){while(scanf("%d",&n)!=EOF&&n){ char c; k=0; int num,dis; for(int i=1;i<=n-1;i++) { scanf(" %c%d",&c,&num); int from=c-'A'+1; while(num--){ getchar(); scanf(" %c%d",&c,&dis); edge[k].u=from; edge[k].v=c-'A'+1; edge[k].w=dis; //cout<<edge[k].u<<" "<<edge[k].v<<endl; k++;} } int ans = kruskal(); printf("%d\n",ans);}return 0;}
0 0
- poj 1251
- POJ 1251
- poj 1251
- poj-1251
- poj 1251
- poj 1251
- POJ 1251
- poj 1251
- POJ 1251
- poj-1251
- poj 1251
- POJ 1251
- POJ 1251
- POJ 1251 Jungle Roads
- poj 1251 Jungle Roads
- poj 1251 Prim算法
- poj 1251 Jungle Roads
- POJ-1251 jungle roads
- Java NIO:NIO概述
- 10817 - Headmaster's Headache(校长的烦恼)
- Cocos2d-x 接入分享SDK之【Android下图片路径问题】
- 1031. Hello World for U (20)
- 进入四维空间 Time Machine带你星际穿越
- POJ 1251
- Project Euler:Problem 66 Diophantine equation
- 堆排序算法之JAVA实现
- fidder手机抓包
- flatbuffers C++ 宏代码欣赏
- 【直播预告】7月19日iOS免费公开课答疑,参与送C币!
- android按Menu出现菜单的动画
- Longest Palindromic Substring
- 小胖说事29-----iOS中Navigation中左滑pop页面的三种方法