Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18256 Accepted Submission(s): 6970
1 #include <cstdio> 2 #include <algorithm> 3 #include <iostream> 4 #include <cstring> 5 using namespace std; 6 #define INF 0x3f3f3f3f 7 int map[105][105]; 8 int n,q,a,b; 9 int res; 10 int mincost[105]; 11 int vis[105]; 12 void prim() 13 { 14 mincost[1]=0; 15 while(true) 16 { 17 int v=-1; 18 int u; 19 for(u=1;u<=n;u++) 20 { 21 if(!vis[u]&&(v==-1||mincost[u]<mincost[v])) 22 v=u; 23 } 24 if(v==-1) break; 25 vis[v]=1; 26 res+=mincost[v]; 27 for(u=1;u<=n;u++) 28 mincost[u]=min(map[v][u],mincost[u]); 29 } 30 return; 31 } 32 int main() 33 { 34 int i,j; 35 freopen("in.txt","r",stdin); 36 while(scanf("%d",&n)!=EOF) 37 { 38 res=0; 39 fill(mincost,mincost+105,INF); 40 memset(vis,0,sizeof(vis)); 41 for(i=1;i<=n;i++) 42 for(j=1;j<=n;j++) 43 scanf("%d",&map[i][j]); 44 scanf("%d",&q); 45 for(i=0;i<q;i++) 46 { 47 scanf("%d%d",&a,&b); 48 map[a][b]=map[b][a]=0; 49 } 50 prim(); 51 printf("%d\n",res); 52 } 53 }
Constructing Roads(1102 最小生成树)
原文:http://www.cnblogs.com/a1225234/p/5041077.html