POJ 1129 Channel Allocation

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 14191   Accepted: 7229

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.
 /*-------------超时代码---------------*/
/*
一开始我直接用的dfs没有剪枝,就是dfs每一个点,枚举每一个频道,找到不相邻,就向下dfs,再加上回溯,每次复杂度是n^3,再加上题目询问的数据量有点大,就超时了。*/
/*--------------------------*/
#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#define N 30
struct Edge{
int v,last;
}edge[N*N];
int head[N];
int sum=(<<)-,n,t=;
int flag[N],pd[N];
bool bb=false;
inline void add_edge(int u,int v)
{
++t;
edge[t].v=v;
edge[t].last=head[u];
head[u]=t;
}
inline void input()
{
char s[N];
for(int i=;i<=n;++i)
{
scanf("%s",s+);
int len=strlen(s+);
for(int j=;j<=len;++j)
add_edge(s[]-'A'+,s[j]-'A'+);
}
}
inline void dfs(int k)
{
if(k==n+)
{
int ans=;
for(int j=;j<=n;++j)
if(flag[j]) ans++;
sum=min(ans,sum);
return;
}
for(int i=;i<=n;++i)
{
int biaozhi=true;
for(int l=head[k];l;l=edge[l].last)
{
if(pd[edge[l].v]==i)
{
biaozhi=false;
break;
}
}
if(!biaozhi) continue;
pd[k]=i;
flag[i]++;
dfs(k+);
flag[i]--;
pd[k]=;
} }
int main()
{
while(scanf("%d",&n)==)
{
if(n==) break;
input();
dfs();
printf("%d channels needed.\n",sum);
memset(edge,,sizeof(edge));
memset(head,,sizeof(head));
sum=(<<)-;t=;bb=false;
memset(flag,,sizeof(flag));
}
return ;
}
 /*-------------对于上面那个代码-----------------*/
特殊数据: A:B
B:
C:
D:
E:
F:
G:
H:
I:
用上面的代码来处理这个非常稀疏的图时间是很长的,因为for(i-->n)枚举频道中的if语句几乎始终成立,那么dfs的复杂度就到了n^n的增长速度,当n==8时,已经1.*^8多了,自然会超时,所以必须改为迭代加深搜索。限定搜索的深度,实际上是不会到n的
 /*改成迭代加深搜索之后,速度果然快了许多。
还有一个值得注意的地方:当sum是1的时候,channel是单数形式,其他时候是复数形式(英语不好被坑了)
*/
#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#define N 30
struct Edge{
int v,last;
}edge[N*N];
int head[N];
bool vis[N][N]={false};
int sum=(<<)-,n,t=;
int pd[N];
bool flag=false;
inline void add_edge(int u,int v)
{
if(vis[u][v]||vis[v][u]) return ;
vis[u][v]=true;vis[v][u]=true;
++t;
edge[t].v=v;
edge[t].last=head[u];
head[u]=t;
++t;
edge[t].v=u;
edge[t].last=head[v];
head[v]=t;
}
inline void input()
{
char s[N];
for(int i=;i<=n;++i)
{
scanf("%s",s+);
int len=strlen(s+);
for(int j=;j<=len;++j)
add_edge(s[]-'A'+,s[j]-'A'+);
}
}
inline void dfs(int k,int minn)
{
if(k==n+)
{
flag=true;
return;
}
for(int i=;i<=minn;++i)
{
bool biaozhi=true;
for(int l=head[k];l;l=edge[l].last)
{
if(pd[edge[l].v]==i)
{
biaozhi=false;
break;
}
}
if(!biaozhi) continue;
pd[k]=i;
dfs(k+,minn);
if(flag) return;
pd[k]=;
} }
int main()
{
while(scanf("%d",&n)==)
{
if(n==) break;
input();
for(int i=;i<=n;++i)
{
dfs(,i);
if(flag)
{
sum=i;
memset(pd,,sizeof(pd));
break;
}else memset(pd,,sizeof(pd));
}
if(sum>)
printf("%d channels needed.\n",sum);
else printf("%d channel needed.\n",sum);
memset(edge,,sizeof(edge));
memset(head,,sizeof(head));
sum=(<<)-;t=;
memset(vis,false,sizeof(vis));
flag=false;
}
return ;
}

迭代加深搜索 POJ 1129 Channel Allocation的更多相关文章

  1. POJ 1129 Channel Allocation(DFS)

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13173   Accepted: 67 ...

  2. POJ 1129 Channel Allocation DFS 回溯

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15546   Accepted: 78 ...

  3. poj 1129 Channel Allocation ( dfs )

    题目:http://poj.org/problem?id=1129 题意:求最小m,使平面图能染成m色,相邻两块不同色由四色定理可知顶点最多需要4种颜色即可.我们于是从1开始试到3即可. #inclu ...

  4. POJ 1129 Channel Allocation 四色定理dfs

    题目: http://poj.org/problem?id=1129 开始没读懂题,看discuss的做法,都是循环枚举的,很麻烦.然后我就决定dfs,调试了半天终于0ms A了. #include ...

  5. poj 1129 Channel Allocation

    http://poj.org/problem?id=1129 import java.util.*; import java.math.*; public class Main { public st ...

  6. poj 1129 Channel Allocation(图着色,DFS)

    题意: N个中继站,相邻的中继站频道不得相同,问最少需要几个频道. 输入输出: Sample Input 2 A: B: 4 A:BC B:ACD C:ABD D:BC 4 A:BCD B:ACD C ...

  7. 迭代加深搜索POJ 3134 Power Calculus

    题意:输入正整数n(1<=n<=1000),问最少需要几次乘除法可以从x得到x的n次方,计算过程中x的指数要求是正的. 题解:这道题,他的结果是由1经过n次加减得到的,所以最先想到的就是暴 ...

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

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14601   Accepted: 74 ...

  9. poj 2248 Addition Chains (迭代加深搜索)

    [题目描述] An addition chain for n is an integer sequence with the following four properties: a0 = 1 am ...

随机推荐

  1. [moka同学笔记]yii2场景的使用(摘录)

    前半部分为自己使用的过程,下边为转载的,具体地址见:http://blog.sina.com.cn/s/blog_88a65c1b0101j717.html 1.在model中 public func ...

  2. 解决客户 IE 浏览器"兼容性视图"设置带来的问题

    最近在给客户开发一个 ASP.NET web 报表时,发现客户的 IE8 浏览器上,看网页总是怪怪的. 调查后发现,客户的 IE8 浏览器,统一被设置成"对本地网络使用兼容性视图" ...

  3. EntityFramework4.1开发

    常见问题大概为这几个 一.ef4.1 codeFirst 修改表结构 增加字段等 EF code first需要重新生成库导致数据丢失的问题. 二.ef4.1 没有了edmx等复杂的东西 变得简单 干 ...

  4. Egret Engine(白鹭引擎)介绍及windows下安装

    Egret Engine简要介绍----- Egret Engine(白鹭引擎)[Egret Engine官网:http://www.egret-labs.org/]是一款使用TypeScript语言 ...

  5. ArcGIS Add-in——自动保存编辑

    需求:由于初次使用ArcGIS编辑器不习惯.数据量大造成经常程序未响应.计算机断电等因素,造成编辑的数据没有保存,影响了生产效率,本人根据草色静然的博文,总结了自动保存编辑的实现方法. 分析:自动保存 ...

  6. 高清SDI编码器|上海视涛科技

    SDI编码器(E500)简介 SDI编码器(E500)是上海视涛科技出品的高性能SDI编码产品.该SDI编码器是上海视涛电子完全自主研发,并适用于各种SDI信号的编码采集及网络传输的专用硬件设备.可兼 ...

  7. Hadoop技术内幕(YARN)第4章问题部分答案

    问题1:改写DistributedShell程序,使得每个container运行在不同节点上(目前是随机的,可能运行在任意节点上). 问题2:改写DistributedShell程序,使得某个用户指定 ...

  8. JSP Model模式

    用JSP开发的Web应用模型可以分为Model1和Model2 对于小型的Web应用,通常可以使用模型1来完成. 模型1可以分为两种方式: 一种是完全使用JSP页面来开发Web应用: 另一种是使用JS ...

  9. Android性能优化(一)

    Android性能优化典范 1.大多数用户感知到的卡顿等性能问题的最主要根源都是因为渲染性能. 从设计师的角度,他们希望App能够有更多的动画,图片等时尚元素来实现流畅的用户体验. 但是Android ...

  10. Runtime(动态添加属性)

    下面通过一个实例展示一下Runtime(动态添加属性)的用法 下面对运行时添加属性用到的策略参数进行补充: 这样看来,前面的NSString* name用的策略是retain nonatomic就知道 ...