标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17187 Accepted Submission(s): 6526
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int per[110],map[101][101]; 6 struct node 7 { 8 int b,e,w; 9 }s[10000]; 10 bool cmp(node x,node y) 11 { 12 return x.w<y.w; 13 } 14 void init() 15 { 16 for(int i=1;i<110;i++) 17 per[i]=i; 18 } 19 20 int find(int x) 21 { 22 while(x!=per[x]) 23 x=per[x]; 24 return x; 25 } 26 27 bool join (int x,int y) 28 { 29 int fx=find(x); 30 int fy=find(y); 31 if(fx!=fy) 32 { 33 per[fx]=fy; 34 return true; 35 } 36 return false; 37 } 38 int main() 39 { 40 int n,i,n1,a,b,j; 41 while(scanf("%d",&n)!=EOF) 42 { 43 init(); 44 for(i=1;i<=n;i++) 45 for(j=1;j<=n;j++) 46 scanf("%d",&map[i][j]); 47 scanf("%d",&n1); 48 for(i=0;i<n1;i++) 49 { 50 scanf("%d%d",&a,&b); 51 map[a][b]=0; 52 } 53 int k=0; 54 for(i=1;i<=n;i++) 55 { 56 for(j=i;j<=n;j++) 57 { 58 s[k].b=i; 59 s[k].e=j; 60 s[k].w=map[i][j]; 61 k++; 62 } 63 } 64 sort(s,s+k,cmp); 65 int sum=0; 66 for(i=0;i<k;i++) 67 { 68 if(join(s[i].b,s[i].e)) 69 sum+=s[i].w; 70 } 71 printf("%d\n",sum); 72 } 73 return 0; 74 } 75
标签:
原文地址:http://www.cnblogs.com/Eric-keke/p/4722157.html