标签:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 3060 | Accepted: 1089 |
Description
Input
Output
Sample Input
4 3 2 0 0 0 0 3 3 6 1 2 4 1 3 10 1 4 12 2 3 6 2 4 8 3 4 5 0
Sample Output
6
最小生成树的思想
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<cstdlib> #include<algorithm> #include<queue> #include<vector> #include<stack> using namespace std; int n,m; int fa[205],w[205]; int find(int x) { if(x!=fa[x]) fa[x]=find(fa[x]); return fa[x]; } void init() { for(int i=0;i<=n;i++) fa[i]=i; for(int i=1;i<=n;i++) w[i]=0; } struct node { int x,y,z; }e[20005]; bool cmp(node a,node b) { return a.z<b.z; } bool check() { for(int i=1;i<=n;i++) { if(fa[i]==i&&w[i]<0) return false; } return true; } int main() { while(scanf("%d",&n)!=EOF) { if(n==0) break; init(); int temp; for(int i=1;i<=n;i++) { scanf("%d",&temp); w[i]-=temp; } for(int i=1;i<=n;i++) { scanf("%d",&temp); w[i]+=temp; } scanf("%d",&m); for(int i=1;i<=m;i++) { scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].z); } sort(e+1,e+1+m,cmp); int pos; for(pos=1;pos<=m;pos++) { int fx,fy; fx=find(e[pos].x),fy=find(e[pos].y); if(fx==fy) continue; fa[fx]=fy; w[fy]+=w[fx]; if(check()) break; } if(pos==m+1) printf("No Solution\n"); else printf("%d\n",e[pos].z); } return 0; }
标签:
原文地址:http://www.cnblogs.com/water-full/p/4487214.html