浙江财经大学
信工学院ACM集训队

POJ 1129 Channel Allocation

本文由 Ocrosoft 于 2016-10-31 16:59:14 发表
Channel Allocation

Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu

Description

When a radio station is broadcasting over a very large area, repeaters are used to retransmit the signal so that every receiver has a strong signal. However, the channels used by each repeater must be carefully chosen so that nearby repeaters do not interfere with one another. This condition is satisfied if adjacent repeaters use different channels. 
Since the radio frequency spectrum is a precious resource, the number of channels required by a given network of repeaters should be minimised. You have to write a program that reads in a description of a repeater network and determines the minimum number of channels required.

Input

The input consists of a number of maps of repeater networks. Each map begins with a line containing the number of repeaters. This is between 1 and 26, and the repeaters are referred to by consecutive upper-case letters of the alphabet starting with A. For example, ten repeaters would have the names A,B,C,…,I and J. A network with zero repeaters indicates the end of input. 
Following the number of repeaters is a list of adjacency relationships. Each line has the form: 
A:BCDH 
which indicates that the repeaters B, C, D and H are adjacent to the repeater A. The first line describes those adjacent to repeater A, the second those adjacent to B, and so on for all of the repeaters. If a repeater is not adjacent to any other, its line has the form 
A: 
The repeaters are listed in alphabetical order. 
Note that the adjacency is a symmetric relationship; if A is adjacent to B, then B is necessarily adjacent to A. Also, since the repeaters lie in a plane, the graph formed by connecting adjacent repeaters does not have any line segments that cross. 

Output

For each map (except the final one with no repeaters), print a line containing the minumum number of channels needed so that no adjacent channels interfere. The sample output shows the format of this line. Take care that channels is in the singular form when only one channel is required.

Sample Input

2
A:
B:
4
A:BC
B:ACD
C:ABD
D:BC
4
A:BCD
B:ACD
C:ABD
D:ABC
0

Sample Output

1 channel needed.
3 channels needed.
4 channels needed. 

Solution

题意:给一个无向图,给每个节点上色,要求相邻节点不能同色,问最少需要多少种颜色。
思路:依次给节点上色,涂颜色的时候,判断这个节点相邻的颜色是否重复,重复就下一种颜色。如果所有已经用过的颜色都不能涂,就添加一种颜色。

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <ctime>
#include <string>
#include <cstdio>
#include <vector>
#include <cctype>
#include <climits>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
#define strend string::npos
#define ms(a) memset(a,0,sizeof(a))
typedef long long LL;
const LL LINF = LLONG_MAX / 2;
const int INF = INT_MAX / 2;
const int MAXN = 100000 + 10;
const int MOD = 1000000009;
int gcd(int a, int b)
{
	if (!b)return a;
	return gcd(b, a%b);
}
using namespace std;
//or2..or2..or2..or2..//
vector<int> v[30];
int colors[30];
bool flag = false;
bool check(int n,int color)
{
	for (int i = 0, len = v[n].size(); i < len; i++)
		if (colors[v[n][i]] == color)return false;
	return true;
}
int n,ans;
void dfs(int id,int tot)
{
	if (flag)return;
	if (id >= n) { flag = true; return; }

	for (int i = 1; i <= tot; i++)
	{
		if (check(id, i))
		{
			colors[id] = i;
			dfs(id + 1, tot);
			colors[id] = 0;
		}
	}
	if (!flag)//需要新的颜色
	{
		ans++;
		dfs(id, tot + 1);
	}
}
int main()
{
	while (cin >> n && n)
	{
		//init
		for (int i = 0; i < 30; i++)v[i].clear();
		ms(colors);
		ans = 1;
		flag = 0;

		for (int i = 0; i < n;i++)
		{
			string s; cin >> s;
			for (int i = 2, len = s.size(); i < len; i++)
				v[s[0] - 'A'].push_back(s[i] - 'A');
		}

		dfs(0, 1);
		printf("%d channel%s needed.\n", ans, ans == 1 ? "" : "s");
	}
	return 0;
}

欢迎转载,请保留出处与链接。Ocrosoft » POJ 1129 Channel Allocation

点赞 (0)or拍砖 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址