标签:des style blog color os io for ar div
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 6 using namespace std; 7 8 #define inf 99999999 9 10 int map[105][105],path,node[105],vis[105],n; 11 12 void Prim() 13 { 14 int ans=1; 15 vis[1]=1; 16 node[1]=0; 17 for(int k=1;k<=n;k++) 18 { 19 int minn=inf; 20 for(int i=1;i<=n;i++) 21 if(!vis[i]&&minn>node[i]) 22 { 23 minn=node[i]; 24 ans=i; 25 } 26 vis[ans]=1; 27 path+=node[ans]; 28 29 for(int i=1;i<=n;i++) 30 { 31 if(!vis[i]&&node[i]>map[ans][i]) 32 node[i]=map[ans][i]; 33 } 34 } 35 } 36 37 int main() 38 { 39 while(cin>>n) 40 { 41 for(int i=1;i<=n;i++) 42 { 43 node[i]=inf;vis[i]=0; 44 for(int j=1;j<=n;j++) 45 cin>>map[i][j]; 46 } 47 int m; 48 cin>>m; 49 while(m--) 50 { 51 int a,b; 52 cin>>a>>b; 53 map[a][b]=map[b][a]=0; 54 } 55 path=0; 56 Prim(); 57 cout<<path<<endl; 58 } 59 return 0; 60 }
hdu1102 Constructing Roads (简单最小生成树Prim算法)
标签:des style blog color os io for ar div
原文地址:http://www.cnblogs.com/mis-xiao/p/3931064.html