poj1129Channel Allocation

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 12844   Accepted: 6579

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. 

Source

Southern African 2001

转载请注明出处:優YoU http://user.qzone.qq.com/289065406/blog/1303954302

题目翻译:

当一个广播电台在一个非常大的地区,广播站会用中继器来转播信号以使得每一个接收器都能接收到一个强烈的信号。然而,每个中继器必须慎重选择使用,使相邻的中继器不互相干扰。如果相邻的中继器使用不同的频道,那么就不会相互干扰。

由于无线电频道是一有限的,一个给定的网络所需的中继频道数目应减至最低。编写一个程序,读取一个中继网络,然后求出需要的最低的不同频道数。

建模:

一个有N个节点的无向图,要求对每个节点进行染色,使得相邻两个节点颜色都不同,问最少需要多少种颜色?

那么题目就变成了一个经典的图的染色问题。

解题思路:

对于这题数据范围很小(节点最多26个),所以使用普通的暴力搜索法即可

对点i的染色操作:从最小的颜色开始搜索,当i的直接相邻(直接后继)结点已经染过该种颜色时,搜索下一种颜色。

就是说i的染色,当且仅当i的临近结点都没有染过该种颜色,且该种颜色要尽可能小。

要注意题中的一句话

since the repeaters lie in a plane, the graph formed byconnecting adjacent repeaters does not have any line segments that cross.

大致意思就是 “城市所在的世界是一个平面世界,当把城市看做点,相邻城市用边连接时,这些边不能相交”

 

PS: 在网上很多同学都说用“四色定理”解决这题, 其实不是,四色定理在本题单纯是用来剪枝的,而且由于结点数较少(只有26),剪枝与否对时间影响不大,普通的爆搜也是0ms AC的,不过有兴趣的同学可以看看“四色定理”。

因为当结点数很多时,四色定理的剪枝优势就会体现出来了

我把 暴力搜索经过四色定理剪枝的搜索 两个程序都都贴出来,大家比较一下就知道四色定理怎么用了。

附:

四色定理的“相邻”是指两块多边形地区“至少一条边重合”才为之相邻

“至少一条边重合”同时也隐含了“任意边(线段)不正规相交

如:

再反观本题的模型,本题的相邻是“两点有边连接,但任意两边不交叉(正规相交)”,这种“相邻”其实就是四色定理的“相邻”。

我举一个例子就一目了然了:

N=7

A:BCDEFG

B:ACDEFG

C:ABD

D:ABCE

E:ABDF

F:ABEG

G:ABF

画成图就是:

PS:由于边不允许相交,这已经是7个点的最大连接数

四色定理的原始理论依据:

对于一个散点集,若要求尽可能连接任意两个点,但任意一条边边不允许与其他边相交,

那么当散点集的元素个数<=4时,连接所得的图必为一个一个 无向完全图

当散点集的元素个数>4时,连接所得的图必不是一个完全图

完全图:任意两点均相邻

最后千万要注意输出,当频道数大于1时,channel为复数 channels

四色原理就是最多四种颜色能够涂满整个图,使得任意相邻多边形的颜色不同

#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
bool vis[26];
int color[26];
int main()
{
	int n;
	while(cin>>n)
	{
		if(n==0)
			return 0;
		vector<int>edge[26];
		while(n--)
		{
			string s;
			cin>>s;
			int from;
			for(int i=0;i<s.length();i++)
			{
				if(i==0)
					from=s[i]-'A';
				else if(i>1)
				{
					int to=s[i]-'A';
					edge[from].push_back(to);
				}
			}
		}
		memset(color,-1,sizeof(color));
		int ans=0;
		for(int i=0;i<26;i++)
		{
			if(color[i]>-1)
				continue;
			memset(vis,0,sizeof(vis));
			for(int j=0;j<edge[i].size();j++)
				if(color[edge[i][j]]>-1)
					vis[color[edge[i][j]]]=1;
			int t;
			for(t=0;t<26;t++)
				if(!vis[t])
					break;
			color[i]=t;
			ans=max(ans,t);
			if(t==3)
				break;
		}
		if(ans==0)
			printf("%d channel needed.\n",ans+1);
		else
			printf("%d channels needed.\n",ans+1);
	}
}
时间: 2024-08-05 17:47:57

poj1129Channel Allocation的相关文章

POJ1129Channel Allocation[迭代加深搜索 四色定理]

Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14601   Accepted: 7427 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 s

POJ--1129--Channel Allocation【顺序着色法】

链接:http://poj.org/problem?id=1129 题意:当一个广播站向一个很广的地区广播时需要使用中继器,用来转发信号,使得接收器都能收到足够强的信号.然后,每个中继器所使用的频道必须很好的选择,以保证相邻的中继器不会互相干扰.要满足这个条件,相邻中继器必须使用不同的频道.但是频道是很宝贵的,对于一个中继器网络,所使用的频道应当尽量少,现在告诉你中继网络信息,求最少需要使用多少个频道. 思路:根据题目建图,把中继器看做点,有边相连的两个点应当使用不同的频道,所以这是一个平面图顶

【转】POJ1129-Channel Allocation:DFS 四色定理 剪枝枚举

#include<iostream> #include<cstdio> #include<cstring> using namespace std; bool g[26][26]; int used[26]; int n; //id 是当下着色结点 color是限制的颜色数量 bool dfs(int id, int color) { bool flag;// 标记 节点id 染第i种色成功与否 for(int i=0; i<color; i++) { flag=

搜索专讲

搜索专讲 Tags:搜索 https://www.zybuluo.com/xzyxzy/note/1058215 前言 做一个专题肯定是要花点时间的 但是哇,搜索怎么这么多内容?!WTF?! 好吧慢慢刷,待四五月份左右出pdf或ppt的讲义吧 先把题目放上,大家愿意的和我一起做吧 题目 李老师给了一个包 广搜 [x] ?POJ1426-Find The Multiple https://vjudge.net/problem/POJ-1426 [x] POJ2251-Dungeon Master

SQL Server -&gt;&gt; Memory Allocation Mechanism and Performance Analysis(内存分配机制与性能分析)之 -- Minimum server memory与Maximum server memory

Minimum server memory与Maximum server memory是SQL Server下配置实例级别最大和最小可用内存(注意不等于物理内存)的服务器配置选项.它们是管理SQL Server内存的途径之一. Minimum server memory与Maximum server memory Minimum server memory(MB): 最小服务器内存.一旦超过这个线就不会再把内存换回去.但是也不是说SQL Server一启动马上就申请这么多的内存. Maximum

AC日记——[USACO10MAR]仓配置Barn Allocation 洛谷 P1937

[USACO10MAR]仓配置Barn Allocation 思路: 贪心+线段树维护: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 #define INF 0x3f3f3f3f #define maxtree maxn<<2 struct QueryType { int l,r; bool operator<(const QueryType pos)const { if(pos.

[转]Under the covers: IAM chains and allocation units in SQL Server 2005

(I'm sitting here in Seattle airport at 7am on Sunday waiting to catch the same flight to Boston that I caught two weeks ago. Instead of TechEd, this time I'm going to a training course at MIT. I'd enjoy the air travel a lot more with a bigger gap in

xv6-----lazy page allocation

本文转载是网络,只叙述方法,,, 第一问:Turn off page allocation in xv6 修改sysproc.c中的sys_sbrk()函数即可: 1 int sys_sbrk(void) 2 { 3 int addr; 4 int n; 5 if(argint(0, &n) < 0) 6 return -1; 7 addr = proc->sz; 8 proc->sz += n; 9 //if(growproc(n) < 0) 10 // return -

[C++] 2D Array&#39;s memory allocation

2D Array's memory allocation [C++] 2D Array's memory allocation