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

类似的题目,我总是用回溯,结果总是 TLE,请教高手,应该怎么做

Posted by IceAngel at 2005-05-27 11:34:51 on Problem 1014
类似的题目,我总是用回溯,结果总是 TLE,请教高手,应该怎么做。

#include "iostream.h"

#define max 6

int graph[max];
int map[max];
int total;		//总和,加权后

bool check()
{
	int i;
	int t = 0;
	for( i = 0 ; i < max ; i++ )
		t += ( i + 1 ) * map[i];
	if( t == total / 2 )
		return true;
	else
		return false;
}

int main()
{
	int i;
	int sum;		//个数
	bool flag;
	int top;
	int caseCount = 0;
	
	sum = 0;
	total = 0;
	for( i = 0 ; i < max ; i++ )
	{
		cin >> graph[i];
		sum += graph[i];
		total += ( i + 1 ) * graph[i];
	}
	while( sum > 0 )
	{
		caseCount++;
		cout<<"Collection #"<<caseCount<<":"<<endl;
		if( total % 2 == 0 )
		{
			for( i = 0 ; i < max ; i++ )
				map[i] = -1;
			top = 0;
			flag = false;//没有找到分法
			do
			{
				map[top]++;
				if( map[top] > graph[top] )
				{
					map[top] = -1;
					top--;
				}
				else
				{
					if( top == max - 1 )
					{
						if( check() )
						{
							cout<<"Can be divided."<<endl;
							flag = true;
						}
					}
					else
					{
						top++;
					}
				}
			}while( top >= 0 && !flag );
			if( !flag )
				cout<<"Can't be divided."<<endl;
		}		
		else
		{
			cout<<"Can't be divided."<<endl;
		}
		cout<<endl;
		//==================================
		//下一循环
		sum = 0;
		total = 0;
		for( i = 0 ; i < max ; i++ )
		{
			cin >> graph[i];
			sum += graph[i];
			total += ( i + 1 ) * graph[i];
		}
	}
	
	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