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

题目太BT 了,差不多的代码,一个AC , 一个TLE,另附有N=20的测试数据

Posted by 421 at 2009-05-20 18:57:28 on Problem 2531
TLE的代码
#include <stdio.h>

int n,a[22][22],max;
bool use[22];

int power() //就这个函数不一样而已
{
	int i,j,ans=0;
	for(i=1;i<=n;i++)
	{
		if(use[i]) continue;
		for(j=1;j<=n;j++)
		{
			if(i==j||use[j]==0) continue;
			ans+=a[i][j];
		}
	}
	return ans;
}
void dfs(int p)
{
	if(p>n)
	{
		if(max<power())
			 max=power();
		return ;
	}
	use[p]=1;
	dfs(p+1);
	use[p]=0;
	dfs(p+1);
}
int main()
{
	int i,j;
	scanf("%d",&n);
	for(i=0;i<22;i++) use[i]=0;
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
			scanf("%d",&a[i][j]);
		max=0;
		dfs(1);
		printf("%d\n",max);
	return 0;
}

/*
20
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
*/

AC了的   
如果你是大牛的话,告诉我为什么???????????????????

#include <stdio.h>

int n,a[22][22],max;
bool use[22];

int power()
{
	int i,j,ans=0;
	for(i=1;i<=n;i++)
	{
		if(use[i]) 
		for(j=1;j<=n;j++)
		{
			if(use[j]==0) 
			ans+=a[i][j];
		}
	}
	return ans;
}
void dfs(int p)
{
	if(p>n)
	{
		if(max<power())
			 max=power();
		return ;
	}
	use[p]=1;
	dfs(p+1);
	use[p]=0;
	dfs(p+1);
}
int main()
{
	int i,j;
	scanf("%d",&n);
	for(i=0;i<22;i++) use[i]=0;
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
			scanf("%d",&a[i][j]);
		max=0;
		dfs(1);
		printf("%d\n",max);
	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