标签:
3 3 1 2 1 1 3 2 2 3 4 1 3 2 3 2 0 100
3 ?
#include <iostream> #include <queue> using namespace std; int n,m,i,flag; int sum; int team[105]; struct node { int x,y,w; node(int a,int b,int c){x=a;y=b;w=c;} }; struct cmp { bool operator()(node a,node b) { return a.w>b.w; } }; int findteam(int k) { if (team[k]!=k) { team[k]=findteam(team[k]); return team[k]; } else return k; } int main() { while (cin>>n>>m&&n) { for(int i=1;i<=m;i++) team[i]=i; priority_queue<node,vector<node>,cmp> path; for(int i=0;i<n;i++) { int x,y,w; cin>>x>>y>>w; path.push(node(x,y,w)); } sum=0; flag=0; while(!path.empty()) { node p=path.top(); path.pop(); int fx=findteam(p.x); int fy=findteam(p.y); if (fx!=fy) { team[fx]=fy; sum+=p.w; } int k=findteam(1); for(i=2;i<=m;i++) if (k!=findteam(team[i])) break; if (i>m) {flag=1; break;} } if (flag) cout<<sum<<endl; else cout<<"?"<<endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/stepping/p/5723076.html