poj 1129 Channel Allocation(DFS减枝)
2015-08-21 14:10
411 查看
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
Sample Output
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.
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <queue> #include <stack> #include <set> #include <map> #include <cmath> #include <algorithm> #define RR freopen("in.txt","r",stdin) #define WW freopne("out.txt","w",stdout) #define LL long long #define INF 0x3f3f3f3f using namespace std; int n; char str[30][30]; bool vis[30][30]; int Color[30]; int cnt; bool Judge(int pos,int color) { for(int i=0; i<n; i++) { if(vis[pos][i] && Color[i] == color) return false; } return true; } void DFS(int pos) { if(pos == n) { set<int >S; for(int i=0; i<n; i++) { S.insert(Color[i]); } int t = S.size(); cnt = min(cnt,t); return ; } if(Color[pos] == -1) { for(int i=0; i<n; i++) { if(Judge(pos,i)) { Color[pos] = i; DFS(pos+1); } } } } int main() { std::ios::sync_with_stdio(false); while(cin>>n && n) { memset(vis,false,sizeof(vis)); for(int i=0; i<n; i++) { cin>>str[i]; for(int j=2; str[i][j]; j++) { vis[i][str[i][j] - 'A'] = true; } } memset(Color,-1,sizeof(Color)); cnt = INF; DFS(0); if(cnt == 1) printf("1 channel needed.\n"); else printf("%d channels needed.\n",cnt); } return 0; }
相关文章推荐
- 第一份软件测试实习(1)
- linux文件压缩命令
- UTF16和UTF8
- JSFF2 mount fail
- Jasper Report(3)--- 用JavaBean Collection做为数据源
- TS流判断I帧
- 基于Spring可扩展Schema提供自定义配置支持(spring配置文件中 配置标签支持)
- POJ 3592--Instantaneous Transference【SCC缩点新建图 && SPFA求最长路 && 经典】
- MapReduce实现join操作
- Maven POM描述参考
- HFile存储格式
- 黑马程序员-[C语言]学习日记(一)
- Spark:一个高效的分布式计算系统--转
- WCF快速上手
- poj 2356 Find a multiple (剩余类,抽屉原理)
- Spring 3 JavaConfig @Import example
- HDOJ 5410 CRB and His Birthday DP背包
- mybatis之foreach用法
- 简单路由器的实现
- VMware与主机三种通信方式