3 1 2 1 0 1 3 2 0 2 3 4 0 3 1 2 1 0 1 3 2 0 2 3 4 1 3 1 2 1 0 1 3 2 1 2 3 4 1 0
3 1 0
在考虑这道题,已经修过的路要不要用的时候,仔细一想,既然修过了,让她变成花费的钱变成0不久ok了,然后就过了.、
附ac代码:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct node { int start; int end; int cost; } t[10010]; int cmp(node a,node b) { return a.cost <b.cost ; } int per[110]; int find(int x) { int r=x; while(r!=per[r]) r=per[r]; return r; } int join(int x,int y) { int fx=find(x); int fy=find(y); if(fx!=fy) { per[fx]=fy; return 1; } return 0; } int main() { int n,m,i,flag; while(scanf("%d",&n),n) { for(i=1;i<110;i++) per[i]=i; m=n*(n-1)/2; for(i=0;i<m;i++) { scanf("%d%d%d%d",&t[i].start,&t[i].end,&t[i].cost,&flag); if(flag)//除了这里跟上一个一模一样. t[i].cost=0; } sort(t,t+m,cmp); int sum=0; for(i=0;i<m;i++) { if(join(t[i].start,t[i].end)) sum=sum+t[i].cost; } printf("%d\n",sum); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/z8110/article/details/47416917