#include<stdio.h> #include<string.h> #include<iostream> using namespace std; const int N=30; int map[N][N]; bool vis[N]; void Prim(int n) { int len=0,count=0; vis[0] = 1; while(count<n-1) { int tmp=-1,u; for(int i=0;i<n;++i){ if(!vis[i]) continue; for(int j=0;j<n;++j) if(map[i][j]!=-1&&!vis[j]&&(map[i][j]<tmp||tmp==-1)) tmp=map[i][j],u=j; } if(tmp!=-1) { ++count; vis[u]=1; len+=tmp; } } cout<<len<<endl; } int main() { int n; char node[2]; while(cin>>n&&n) { memset(map,-1,sizeof(map)); memset(vis,0,sizeof(vis)); int m=n-1; while(m--) { int count; cin>>node>>count; int a=node[0]-'A'; while(count--) { int len; cin>>node>>len; int b=node[0]-'A'; map[a][b]=map[b][a]=len; } } Prim(n); } return 0; }
Jungle Roads HDU - 1301 prim
猜你喜欢
转载自www.cnblogs.com/QingyuYYYYY/p/12240571.html
今日推荐
周排行