标签:
链接:
http://poj.org/problem?id=1287
代码:
#include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> using namespace std; const int N = 210; const int INF = 0xfffffff; int n; int J[N][N], dist[N]; bool vis[N]; int Prim() { int i, j, ans=0; dist[1]=0; memset(vis, 0, sizeof(vis)); vis[1]=1; for(i=1; i<=n; i++) dist[i]=J[1][i]; for(i=1; i<n; i++) { int index=1; int MIN=INF; for(j=1; j<=n; j++) { if(!vis[j] && dist[j]<MIN) { index=j; MIN=dist[j]; } } vis[index]=1; ans += MIN; for(j=1; j<=n; j++) { if(!vis[j] && dist[j]>J[index][j]) dist[j]=J[index][j]; } } return ans; } int main () { while(scanf("%d", &n), n) { int m, i, j, a, b, t; scanf("%d", &m); for(i=1; i<=n; i++) for(j=1; j<=i; j++) J[i][j]=J[j][i]=INF; for(i=1; i<=m; i++) { scanf("%d%d%d", &a, &b, &t); J[a][b]=J[b][a]=min(J[a][b], t); } int ans=Prim(); printf("%d\n", ans); } return 0; }
(最小生成树) Networking -- POJ -- 1287
标签:
原文地址:http://www.cnblogs.com/YY56/p/4735018.html