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竟然0ms AC~

Posted by intheway at 2009-06-23 14:50:11 on Problem 1129
//1129 Channel Allocation
//DFS, 用vector记录某个信道编号被用的情况, 每次当选中此信道时, 查看与vector中的点是否邻接即可
//最先得到的答案即为最优解
#include <iostream>
#include <vector>
using namespace std;
const int SIZE = 30;
bool graph[SIZE][SIZE];
char save[SIZE];
int n;
int cnt;
vector<int> vec[30];
bool suss;

bool Select(int k, int deep)
{

	for (int j = 0; j != vec[k].size(); ++j)
	{
		if(graph[deep][vec[k][j]])
			return false;
	}
	return true;
}

void DFS(int deep)
{
	if(suss)
		return ;
	if(deep == n)//处理完所有点
	{
		suss = true;
		return ;
	}
	for (int k = 0; k < n; ++k)
	{
		if(suss)//这里要添加,因为当已经取得解时, 仍然会进入循环!!
			return ;
		if (Select(k, deep))
		{
			if(vec[k].size() == 0)//被选的信道没被用过
			{
				++cnt;
			}
			vec[k].push_back(deep);
			DFS(deep+1);
		}
	}
}

int main()
{
	freopen("in.txt", "r", stdin);
	while (scanf("%d", &n) && n != 0)
	{
		int i, j;
		for(i = 0; i < SIZE; ++i)
			memset(&graph[i], false, sizeof(graph[i]));
		cnt = 0;
		suss = false;
		for(i = 0; i < n; ++i)
			vec[i].clear();
		for (i = 0; i < n; ++i)
		{
			scanf("%s", save);
			for(j = 2; save[j] != '\0'; ++j)
			{
				graph[save[0]-'A'][save[j]-'A'] = true;
			}
		}

		DFS(0);
		if(cnt == 1)
			printf("%d channel needed.\n", cnt);
		else
			printf("%d channels needed.\n", 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