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 lxhgww at 2006-12-09 20:36:00 on Problem 3140
In Reply To:嗯...m的值给的很恶心..... Posted by:FinalLaugh at 2006-12-09 20:30:47
#include <stdio.h>
#include <vector>

using namespace std;

const long MAX_N=100001;
const long long inf=1000001LL;

bool arr[MAX_N];
vector<long> conn[MAX_N];
long long zz[MAX_N];
long n;
long long nowMin,sum;

long long search(long w)
{
	long long tot=zz[w];
	long long maxBlock=inf;
	arr[w]=true;
	for (vector<long>::iterator i=conn[w].begin();i!=conn[w].end();i++)
		if (!arr[*i])
		{
			long long tmp=search(*i);
			if (tmp>=sum-tmp)
			{
				if (maxBlock>2*tmp-sum) maxBlock=2*tmp-sum;
			}
			else
			{
				if (maxBlock>sum-2*tmp) maxBlock=sum-2*tmp;
			}
			tot+=tmp;
		}
	if (tot>=sum-tot)
	{
		if (maxBlock>2*tot-sum) maxBlock=2*tot-sum;
	}
	else
	{
		if (maxBlock>sum-2*tot) maxBlock=sum-2*tot;
	}
	if (maxBlock<nowMin) nowMin=maxBlock;
	return tot;
}

int main()
{
	long i,m,a,b,pp;
	pp=1;
	scanf("%ld%ld",&n,&m);
	while (!(n==0&&m==0))
	{
		for (i=1;i<=n;i++)
			conn[i].clear();
		sum=0;
		for (i=1;i<=n;i++)
		{
			scanf("%I64d",&zz[i]);
			sum=sum+zz[i];
		}
	    for (i=1;i<=m;i++)
		{
			scanf("%ld%ld",&a,&b);
		    conn[a].push_back(b);
		    conn[b].push_back(a);
		}
		memset(arr,false,sizeof(arr));
	    nowMin=inf;
	    search(1);
	    printf("Case %ld: %I64d\n",pp,nowMin);
		pp++;
        scanf("%ld%ld",&n,&m);
	}
	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