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 RUNSLOWLY at 2009-08-11 21:55:03 on Problem 2531 and last updated at 2009-08-11 21:56:28
In Reply To:题目太BT 了,差不多的代码,一个AC , 一个TLE,另附有N=20的测试数据 Posted by:421 at 2009-05-20 18:57:28
#include<iostream>
using namespace std;
int add_one(int b[],int n)
{
	b[n]++;
	int i;
	i=n;
	while(b[i]>1 && i>0)
	{
		b[i-1]++;
		b[i]=0;
		i--;
	}
	return b[0];
}



int main()
{
	int b[21];//表示被分配到哪一组。可将其表示为一个二进制数,从1开始。
	int g[21][21]; //表示路径。
	int n,i,j,sum,max;
	while(cin>>n)
	{
		if(n<=1)
			break;
		for(i=1;i<=n;i++)
			for(j=1;j<=n;j++)
				cin>>g[i][j];
		memset(b,0,sizeof(b));
		add_one(b,n);
		max=0;
		while(b[0]==0)
		{
			sum=0;
			for(i=1;i<=n;i++)
			{
				if(b[i]==0)
				{
					for(j=1;j<=n;j++)
					{
						if(b[j]==1)
							sum+=g[i][j];
					}
				}
			}
			if(sum>max)
				max=sum;
			if(b[1]==1)    //很好的对称剪支,速度快了一倍,要不然会超时。
				break;
			add_one(b,n);
		}
		cout<<max<<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