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. [PHP] 实现路由映射到指定控制器

    自定义路由的功能,指定到pathinfo的url上,再次升级之前的脚本 SimpleLoader.php <?php class SimpleLoader{ public static func ...

  2. 初学者对WAMP服务器的设置

    服务器设置 在wamp/bin/apache/Apache###/conf/httpd.conf文件中设置 根文件夹 修改documentroot和directory两项 保存后重启服务 404返回值 ...

  3. 【原创】使用.NET Core 1.0创建一个Self-Contained控制台应用

    开发机器:win7-x64 .NET Core版本:1.0.0-preview2-003121 Visual Studio Code:1.2.1 至于什么是Self-Contained应用类型以及与P ...

  4. [js开源组件开发]图片放大镜

    图片放大镜 一般情况下,手机由于屏幕太小,会有图片上看不清的问题,所以我就做了一个放大镜的js效果,支持pc和移动端.它的原理是利用的backgroundsize来实现的,所以你的浏览器首先要支持这个 ...

  5. Mvc项目架构分享之项目扩展

    Mvc项目架构分享之项目扩展 Contents 系列一[架构概览] 0.项目简介 1.项目解决方案分层方案 2.所用到的技术 3.项目引用关系 系列二[架构搭建初步] 4.项目架构各部分解析 5.项目 ...

  6. ABAP常用函数集锦

    函数名 描述 SD_VBAP_READ_WITH_VBELN 根据销售订单读取表vbap中的信息EDIT_LINES 把READ_TEXT返回的LINES中的行按照TDFORMAT=“*”重新组织VI ...

  7. Oracle SQL Tips

    左连接的同时只输出关联表的一条记录 WITH X AS (SELECT 1 ID FROM DUAL UNION SELECT 2 FROM DUAL UNION SELECT 3 FROM DUAL ...

  8. 浅谈RSA加密算法

    一.什么是非对称加密 1.加密的密钥与加密的密钥不相同,这样的加密算法称之为非对称加密 2.密钥分为:公钥,私钥  公钥:可以对外给任何人的加密和解密的密码,是公开的 私钥:通过私钥可以生成公钥,但从 ...

  9. C语言指针的长度和类型

    本文地址:http://www.cnblogs.com/archimedes/p/point-length-type.html,转载请注明源地址. 如果考虑应用程序的兼容性和可移植性,指针的长度就是一 ...

  10. Mac搭建本地svn服务器,并用Cornerstone连接服务器

    Mac默认已经安装了svn,我们只需要进行配置并开启就可以了 首先我们可以验证一下是否安装了svn,打开终端,输入命令 svnserve --version 这里可以看到目前svn的版本号,说明已经安 ...