hdu_1301 Jungle Roads
2015-04-25 09:47
253 查看
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #define N 100005 using namespace std; int father ; struct node { int u,v,w; }save ; int cmp(node a,node b) { return a.w < b.w; } void Make_set(int n) { int i; for(i=0; i<=n; i++) { father[i] = i; } } int Find_set(int x) { int pre,ans,temp; pre = x; while(x != father[x]) { x = father[x]; } ans = x; while(pre != father[pre]) { temp = father[pre]; father[pre] = x; pre = temp; } return ans; } void Union_set(int a,int b) { int x = Find_set(a); int y = Find_set(b); if(x > y) father[x] = y; else father[y] = x; } int main() { int i,j,k,sum,n,m,x,y,ans,res,value; char s,t; while(cin>>n,n) { sum = 0,k = 0; for(i=0; i<n-1; i++) { cin>>s>>m; for(j=0; j<m; j++) { cin>>t>>value; save[k].u = s - 'A'; save[k].v = t - 'A'; save[k++].w = value; } } sort(save,save+k,cmp); /* for(i=0; i<k; i++) cout<<save[i].u<<" "<<save[i].v<<" "<<save[i].w<<endl; */ Make_set(k); for(i=0; i<k; i++) { x = Find_set(save[i].u); y = Find_set(save[i].v); if(x != y) { Union_set(save[i].u,save[i].v); sum += save[i].w; } } cout<<sum<<endl; } return 0; }
相关文章推荐
- hdu 1301 Jungle Roads poj
- (step6.1.6)hdu 1301(Jungle Roads——最小生成树)
- hdu 1301 Jungle Roads &&poj 1251 (prim 或者kruskal)
- Jungle Roads HDU - 1301 (最小生成树 prim)
- HDU 1301:Jungle Roads【Prim】
- HDU 1301 Jungle Roads
- HDU Jungle Roads 1301 最小生成树、
- hdu 1301 prime Jungle Roads
- hdu1301——Jungle Roads——最小生成树
- HDU 1301 Jungle Roads Kruskal求解
- HDU 1301 Jungle Roads
- hdu 1301 Jungle Roads<java><prime>
- hdu Jungle Roads
- HDOJ&nbsp;&nbsp;1301&nbsp;&nbsp;Jungle&nbsp;Roads
- 杭电1301--Jungle Roads(最小生成…
- hdu Jungle Roads(水题,prim)
- HDU 1025 Constructing Roads In JGShining's Kingdom
- hdu 1025 Cstructing Roads (Constructing Roads In JGShining's Kingdom)
- POJ1251 Jungle Roads Kruskal+scanf输入小技巧
- hdu 1301 Jungle Roads (最小生成树)