您的位置:首页 > 其它

hoj 1632

2012-02-13 19:01 267 查看
#include <iostream>
using namespace std;
const int N=30;
const int Max=105;//cost
int cost

,d
,total,n;
bool f
;

void init()
{
for(int i=1;i<=n;i++)
{
f[i]=0;
d[i]=Max;
for(int j=i+1;j<=n;j++)
{
cost[i][j]=Max;
cost[j][i]=Max;
}
}

return;
}
void path()
{
int k=1;
f[1]=1;
d[1]=0;
total=0;
for(int j=2;j<=n;j++)
{
int mcost=Max;
for(int i=2;i<=n;i++)
{
if(!f[i])
{
if(d[i]<mcost)
{
mcost=d[i];
k=i;
}
}
}//for
f[k]=1;
total+=d[k];
for(int t=2;t<=n;t++)
{
if((!f[t])&&(d[t]>cost[k][t]))
d[t]=cost[k][t];
}
}

return;
}
int main()
{
while(cin>>n)
{
if(n==0) break;
init();
for(int i=1;i<n;i++)
{
int num,weight;
char first,second;
cin>>first>>num;
for(int j=0;j<num;j++)
{
cin>>second>>weight;
cost[first-'A'+1][second-'A'+1]=weight;
cost[second-'A'+1][first-'A'+1]=weight;
}

}//for
for(int k=2;k<=n;k++)
d[k]=cost[1][k];
path();
cout<<total<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: