Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

求测试数据,我的wa了(附代码)

Posted by zhenjie at 2009-08-25 22:37:52 on Problem 1251
#include <iostream>
using namespace std;
typedef struct
{
    int fromvex,endvex;
    int length;
}edge;
edge T[29];
int dist[30][30];
int Prim(int n)
{
    int j,k,m,v;
    int min,max=10000,d,sum=0;
    edge e;
    v=0;
    for(j=0;j<=n-2;j++)
    {
        T[j].fromvex=v;
        if(j>=v)
        {
            T[j].endvex=j+1;
            T[j].length=dist[v][j+1];
        }
        else
        {
            T[j].endvex=j;
            T[j].length=dist[v][j];
        }
    }
    for(k=0;k<n-1;k++)
    {
        min=max;
        for(j=k;j<n-1;j++)
        {
            if(T[j].length<min)
            {
                min=T[j].length;m=j;
            }
        }
        sum+=min;
        e=T[m];T[m]=T[k];T[k]=e;
        v=T[k].endvex;
        for(j=k+1;j<n-1;j++)
        {
            d=dist[v][T[j].endvex];
            if(d<T[j].length)
            {
                T[j].length=d;T[j].fromvex=v;
            }
        }
    }
    return sum;
}
int main()
{
    int i,j,n,sum,t;
    char a[30];
    while(cin>>n&&n!=0)
    {
    for(i=0;i<n;i++)
    for(j=0;j<n;j++)
    dist[i][j]=10000;
    for(i=0;i<n-1;i++)
    {
        cin>>a[0]>>t;
        for(j=1;j<=t;j++)
        {
            cin>>a[j]>>dist[i][a[j]-'A'];
            dist[a[j]-'A'][i]=dist[i][a[j]-'A'];
        }
    }
    sum=Prim(n);
    cout<<sum<<endl;
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator