Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4083 Accepted Submission(s): 1577
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#include <stdio.h> #include <math.h> #include <string.h> #define MAX 105 #define MAXCOST 99999999 int n; int sum; int graph[MAX][MAX]; void Prim() { int i,j,k,min; int lowcost[MAX]; int adjvex[MAX]; for(i=1;i<n;i++) lowcost[i]=graph[0][i]; //从第一个的顶点开始 memset(adjvex,0,sizeof(adjvex)); min=MAXCOST; for(i=1;i<n;i++) { min=MAXCOST; for(j=1;j<n;j++) if(adjvex[j]==false && lowcost[j]<min) { min=lowcost[j]; k=j; //记下最小的点 } adjvex[k]=true; //为true表示该权值已经是最小,为flase是还不确定,应继续更新 for(j=1;j<n;j++) { if(adjvex[j]==false && lowcost[j]>graph[k][j]) //更新lowcost lowcost[j]=graph[k][j]; } } for(i=1;i<n;i++) sum+=lowcost[i]; //这就是最小生成树 } int main() { int m,i,j,a,b,c,d; while (scanf("%d",&n),n) { for (i=0;i<n;i++) { for (j=0;j<n;j++) { graph[i][j] = graph[j][i] = MAXCOST; } } m=n*(n-1)/2; for (i=1;i<=m;i++) { scanf("%d%d%d%d",&a,&b,&c,&d); if(d==0) graph[a-1][b-1]=graph[b-1][a-1]=c; else graph[a-1][b-1]=graph[b-1][a-1]=0; } sum=0; Prim(); printf("%d/n",sum); } return 0; }