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 00848259 at 2009-10-24 11:23:38 on Problem 2054
我先找到Ci最大的点 如果Ci同为最大 就比较父节点的Ci值 大的就让它与它的所有父结点入队列order(已经入队列的就不需要再入了)
要是一直找到根节点他们的Ci值都相同 就将层数较小的那棵字树先入队列order
这个思想是不是本身就有问题 还是程序写错了..

#include<iostream>
using namespace std;
int N,R;
struct tree
{
	int number;
	int Ci;
	int Father;
}node1[1001],node2[1001];
int findfather(int father2,int father1)
{
	if( father2==R && father1!=R )
	{
		return 1;
	}
	else if( father2!=R && father1==R)
	{
		return -1;
	}
	else if( father2==father1)
	{
		return 0;
	}
	else if( node2[father2].Ci != node2[father1].Ci )
	{
		return node2[father2].Ci - node2[father1].Ci;
	}
	else
	{
		return findfather(node2[father2].Father,node2[father1].Father);
	}
}
int compare(const void *elem1,const void *elem2)
{
	tree *p1,*p2;
	p1=(tree*)elem1;
	p2=(tree*)elem2;
	if( p2->Ci != p1->Ci )
	{
		return p2->Ci - p1->Ci;
	}
	else
	{
		return findfather(p2->Father,p1->Father);
	}
}
int main()
{
	int cost;
	int queue[1001],tops;
	int i,j;
	int father,son;
	int isorder[1001];
	int number,order[1001],orders;
	while(cin>>N>>R && N!=0 && R!=0)
	{
		cost=0;
		isorder[R]=1;
		memset(isorder,0,sizeof(isorder));
		memset(order,0,sizeof(order));
		order[0]=R;
		for(i=1;i<=N;i++)
		{
			node1[i].number=i;
			node2[i].number=i;
			cin>>node1[i].Ci;
			node2[i].Ci=node1[i].Ci;
			node1[i].Father=0;
			node2[i].Father=0;
		}
		for(i=1;i<N;i++)
		{
			cin>>father>>son;
			node1[son].Father=father;
			node2[son].Father=father;
		}
		for(i=1;i<=N;i++)
		{
			int tempnumber,tempCi,tempFather;
			if(node1[i].number==R)
			{
				tempnumber=node1[1].number;
				tempCi=node1[1].Ci;
				tempFather=node1[1].Father;
				node1[1].number=node1[i].number;
				node1[1].Ci=node1[i].Ci;
				node1[1].Father=node1[i].Father;
				node1[i].number=tempnumber;
				node1[i].Ci=tempCi;
				node1[i].Father=tempFather;
				break;
			}
		}
		qsort(node1+2,N-1,sizeof(tree),compare);
		orders=1;
		tops=0;
		for(i=2;i<=N;i++)
		{
			number=node1[i].number;
			if(isorder[number]==0)
			{
				while(node2[number].Father!=R && isorder[number]==0)
				{
					queue[tops++]=number;
					isorder[number]=1;
					number=node2[number].Father;
				}
				j=0;
				if(isorder[number]==0)
				{
					order[orders++]=number;
					isorder[number]=1;
				}
				orders+=tops;
				while(j<tops)
				{
					order[--orders]=queue[j];
					j++;
				}
				orders+=tops;
				tops=0;
			}
		}
		for(i=0;i<N;i++)
		{
			number=order[i];
			cost+=node2[number].Ci * (i+1);
		}
		cout<<cost<<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