hdu 1301

来源:互联网 发布:上海网络电视台 编辑:程序博客网 时间:2024/06/13 11:17

这道题目我在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;}