这道题目我在poj上也做过,要做稍微的转化!
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=30;
const int inf=0x3f3f3f3f;
int map[maxn][maxn];
int dist[maxn];
bool p[maxn];
int sum;
int n;
void init()
{
int i,j;
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
map[i][j]=inf;
}
}
}
int prime(int v)
{
int i,j,pos,min;
for(i=1; i<=n; i++)
{
p[i]=false;
dist[i]=map[v][i];
}
p[v]=true;
dist[v]=0;
sum=0;
for(i=1; i<n; i++)
{
min=inf;
for(j=1; j<=n; j++)
{
if(!p[j]&&dist[j]<min)
{
min=dist[j];
pos=j;
}
}
p[pos]=true;
sum+=dist[pos];
for(j=1; j<=n; j++)
{
if(!p[j]&&dist[j]>map[pos][j])
{
dist[j]=map[pos][j];
}
}
}
return sum;
}
int main()
{
while(scanf("%d",&n)!=EOF&&n!=0)
{
init();
int i,j;
char c,d;
int b,a;
for(i=1; i<n; i++)
{
getchar();
cin>>c>>b;
for(j=1; j<=b; j++)
{
cin>>d>>a;
map[d-64][c-64]=a;
map[c-64][d-64]=a;
}
}
int k;
k=prime(1);
cout<<k<<endl;
}
return 0;
}
hdu 1301
最新推荐文章于 2021-04-08 17:54:16 发布