Link:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1586
Http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#problem/E
Code:
#include <cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>using namespacestd;Const intN = +;Const intINF =0xFFFFFFF;intN;intJ[n][n], dist[n];BOOLVis[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; intmin=INF; for(j=1; j<=n; J + +) { if(!vis[j] && dist[j]<min &&Dist[j]) {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 () {intT; scanf ("%d", &t); while(t--) { intI, J, a, b[n]; scanf ("%d", &N); memset (J,0,sizeof(J)); memset (b,0,sizeof(b)); for(i=1; i<=n; i++) {scanf ("%d", &a); B[i]=A; } for(i=1; i<=n; i++) for(j=1; j<=n; J + +) {scanf ("%d", &a); J[I][J]=A; } for(i=1; i<=n; i++) { for(j=1; j<=n; J + +) J[i][j]+ = b[i]+B[j]; } intans=Prim (); printf ("%d\n", ans); } return 0;}
(minimum spanning tree) QS Network--ZOJ--1586