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

终于过了~dfs 200+(代码)

Posted by skyhacker at 2011-01-31 01:12:05 on Problem 2531
#include<iostream>
using namespace std;
int N,dis[21][21],known[21];
int cnt;

int dfs(int k,int d)
{
	int t=d;
	//计算当第k台电脑放进第一组是的数据总量
	for(int i=1;i<=N;i++)
	{
		if(known[i] ==1) t-=dis[k][i];
		else t+=dis[k][i];
	}
	//
	if(t>cnt) cnt=t;

	for(int i=k+1;i<=N;i++)
	{
		if(known[i]==0)
		{
			known[i]=1;		//将第i台电脑放到第一组
			dfs(i,t);
			known[i]=0;		//不放
		}
	}

	return 1;
}

int main()
{
	cin>>N;
	for(int i=1;i<=N;i++)
		for(int j=1;j<=N;j++)
			cin>>dis[i][j];
	memset(known,0,sizeof(known));
	cnt=0;
	dfs(0,0);
	cout<<cnt;
	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