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

帮忙看看为什么不对呢??

Posted by huyizhu at 2009-08-30 18:35:49 on Problem 1251
#include<iostream>
using namespace std;

int Prim(int money[27][27], int n)
{
	/* lowcost[i]记录以i为终点的边的最小权值,当lowcost[i]=0时表示终点i加入生成树 */
	int lowcost[27];
 
	/* mst[i]记录对应lowcost[i]的起点,当mst[i]=0时表示起点i加入生成树 */
	int mst[27];
 
	int i, j, min, minid, sum = 0;
 
	/* 默认选择0号节点加入生成树,从1号节点开始初始化 */
	for (i = 1; i <= n; i++)
	{
		/* 最短距离初始化为其他节点到0号节点的距离 */
		lowcost[i] = money[0][i];
 
		/* 标记所有节点的起点皆为默认的0号节点 */
		mst[i] = 0;
	}
 
	/* 标记0号节点加入生成树 */
	mst[0] = 0;
 
	/* n个节点至少需要n-1条边构成最小生成树 */
	for (i = 1; i <= n; i++)
	{
		min = 101;
		minid = 0;
 
		/* 找满足条件的最小权值边的节点minid */
		for (j = 1; j <= n; j++)
		{
			/* 边权值较小且不在生成树中 */
			if (lowcost[j] < min && lowcost[j] != 0)
			{
				min = lowcost[j];
				minid = j;
			}
		}
		/* 累加权值 */
		sum += min;
 
		/* 标记节点minid加入生成树 */
		lowcost[minid] = 0;
 
		/* 更新当前节点minid到其他节点的权值 */
		for (j = 1; j <= n; j++)
		{
			/* 发现更小的权值 */
			if (money[minid][j] < lowcost[j])
			{
				/* 更新权值信息 */
				lowcost[j] = money[minid][j];
 
				/* 更新最小权值边的起点 */
				mst[j] = minid;
			}
		}
	}
	/* 返回最小权值和 */
	return sum;
}

int main()
{
    int n,m,i,j,k;
    int money[27][27];
    char a,b;
    while(1)
    {
            cin>>n;
            if(n==0) break;
            for(i=1;j<=n;j++)
            for(j=1;j<=n;j++)
            {
                         money[i][j]=101;
            }
            for(i=1;i<=n-1;i++)
            {
                            cin>>a>>m;
                            for(j=1;j<=m;j++)
                            cin>>b>>k;
                            money[a-'A'+1][b-'A'+1]=money[b-'A'+1][a-'A'+1]=k;
            }
     cout<<Prim(money,n)<<endl;
    } 
}
   

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