Link:
http://poj.org/problem?id=2421
Code:
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;Const intN = the;Const intINF =0xFFFFFFF;intN, J[n][n], dist[n], vis[n];intPrim () {intI, 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++) { intindex=1, 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]; } } returnans;}intMain () { while(SCANF ("%d", &n)! =EOF) { intI, J; memset (J,0,sizeof(J)); for(i=1; i<=n; i++) for(j=1; j<=n; J + +) scanf ("%d", &J[i][j]); intm, A, B; scanf ("%d", &m); for(i=1; i<=m; i++) {scanf ("%d%d", &a, &b); J[A][B]=j[b][a]=0; } intans=Prim (); printf ("%d\n", ans); } return 0;}
(minimum spanning tree) constructing Roads--POJ--2421