标签:cas 政府 TE near algo 调查 输出 hid str
Input
Output
Sample Input
Sample Output
#include <cstdio> #include <algorithm> #include <iostream> #include <cstring> #define Maxn 200 #define INF 0x3f3f3f3f int n,m; int lowcast[Maxn]; int nearvex[Maxn]; int edge[Maxn][Maxn]; using namespace std; void prim(int u0) { int i,j; int sumweight=0; for(i=1;i<=n;i++) { lowcast[i]=edge[u0][i]; nearvex[i]=u0; } nearvex[u0]=-1; for(i=1;i<n;i++) { int mins=INF; int v=-1; for(j=1;j<=n;j++) { if(nearvex[j]!=-1 && mins>lowcast[j]) { mins=lowcast[j]; v=j; } } if(v!=-1) { nearvex[v]=-1; sumweight+=mins; for(j=1;j<=n;j++) { if(nearvex[j]!=-1 && lowcast[j] > edge[v][j]) { lowcast[j] = edge[v][j]; nearvex[j]=v; } } } else{ printf("?\n"); return ; } } printf("%d\n",sumweight); return ; } int main() { while(scanf("%d%d",&m,&n)!=EOF && m!=0) { int i,j,a,b,c; memset(edge,0,sizeof(edge)); for(i=0;i<m;i++){ scanf("%d%d%d",&a,&b,&c); edge[a][b]=edge[b][a]=c; } for(i=1;i<=n;i++){ for(j=1;j<=n;j++){ if(i==j) edge[i][j]=0; else if(edge[i][j]==0) edge[i][j]=INF; } } prim(1); } return 0; }
标签:cas 政府 TE near algo 调查 输出 hid str
原文地址:https://www.cnblogs.com/coder-tcm/p/9157137.html