题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301
依旧Prim............不多说了
#include<iostream>
#include<cstdio>
#include<algorithm> using namespace std; #define MAX 'Z'+5
#define inf 99999999 int map[MAX][MAX],node[MAX],vis[MAX],n,path; void Prim()
{
int ans='A';
node[ans]=;
vis[ans]=;
for(int k='A';k<('A'+n);k++)
{
//cout<<k<<" "<<n<<endl;
int minn=inf;
for(int i='A';i<('A'+n);i++)
if(!vis[i]&&minn>node[i])
{
minn=node[i];
ans=i;
}
//printf("%c %d\n",ans,minn);
vis[ans]=;
path+=node[ans];
//cout<<" "<<path<<endl; for(int i='A';i<('A'+n);i++)
if(!vis[i]&&node[i]>map[ans][i])
node[i]=map[ans][i];
}
} int main()
{
while(cin>>n&&n)
{
for(int i='A';i<='Z';i++)
{
node[i]=inf;
vis[i]=;
for(int j='A';j<='Z';j++)
map[i][j]=inf;
}
int N=n-;
char a,b;
int t,dist;
while(N--)
{
cin>>a>>t;
while(t--)
{
cin>>b>>dist;
if(map[a][b]>dist)
map[a][b]=map[b][a]=dist;
}
}
path=;
Prim();
cout<<path<<endl;
}
return ;
}