#include<stdio.h> int arr[101][101]; int main(){ int n,ttt = 0; scanf("%d",&n); for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ scanf("%d",&arr[i][j]); } } for(int j=2;j<=n;j++){ int min = 100000000; int index = 0; for(int i=1;i<=n;i++){ if(arr[1][i]==0) continue; if(arr[1][i]<min){ min = arr[1][i]; index = i; } } ttt += arr[1][index]; for(int i=1;i<=n;i++){ arr[1][i] = arr[1][i]<arr[index][i]?arr[1][i]:arr[index][i]; } } printf("%d\n",ttt); return 0; }
#include<iostream> #include<stdio.h> #include<cstdlib> using namespace std; struct eg{ int start; int end; int weight; }; int addNum = 0; eg arr[5060]; int cmp(const void *v1,const void *v2){ eg t1 = *(eg*)v1; eg t2 = *(eg*)v2; return t1.weight-t2.weight; } int main(){ int n,index=0,ttt; scanf("%d",&n); for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(j<=i){ scanf("%d",&ttt); }else{ scanf("%d",&arr[index].weight); arr[index].start = i; arr[index].end = j; index++; } } } int num = n*(n-1)/2; qsort(arr,num,sizeof(eg),cmp); int countW = 0; int x[101]; for(int i=1;i<=100;i++){ x[i] = i; } for(int i=0;i<num&&addNum<n;i++){ int k,g; for(k=arr[i].start;x[k]!=k;k=x[k]) //判断线段的起始点所在的集合 x[k]=x[x[k]]; for(g=arr[i].end;x[g]!=g;g=x[g]) //判断线段的终点所在的集合 x[g]=x[x[g]]; if(k!=g) //如果线段的两个端点所在的集合不一样 { x[g]=k; countW+=arr[i].weight; addNum++; //printf("最小生成树中加入边:%d %d/n",a[i].m,a[i].n); } } printf("%d\n",countW); return 0; }
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0