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. JPA学习(6)JPQL

    JPQL语言,即 Java Persistence Query Language 的简称.JPQL 是一种和 SQL 非常类似的中间性和对象化查询语言,它最终会被编译成针对不同底层数据库的 SQL 查 ...

  2. [PE结构分析] 8.输入表结构和输入地址表(IAT)

    在 PE文件头的 IMAGE_OPTIONAL_HEADER 结构中的 DataDirectory(数据目录表) 的第二个成员就是指向输入表的.每个被链接进来的 DLL文件都分别对应一个 IMAGE_ ...

  3. bootstrap 分页

    1.背景: 前端页面使用bootstrap分页,同时与搜索条件联动: 2. jsp页面由服务端返回后, 异步请求动态创建表格, 分页的数据由服务端第一次返回后初始化, 以后每次异步请求再更新. jsp ...

  4. (旧)子数涵数·DW——网页制作的流程

    PS:这是我很早以前的一个废掉的项目. 当时用的还是table排版,现在基本都是div了吧. 这个项目前段时间,我还抢救过一次,后来还是放弃了. 先行.网页制作的流程分为哪些呢? 一.网站策划(当时, ...

  5. php学习笔记:读取文档的内容,利用php修改文档内容

    直接上代码 <?php /** * Created by PhpStorm. * User: Administrator * Date: 2016/9/10 0010 * Time: 20:27 ...

  6. 使用PowerQuery操作OData数据

             Excel是我们耳熟的办公软件.PowerQuery是一个允许连接多种数据源的Excel插件.它能从一个网页上智能查询数据.使用PowerQuery能合并数据集使用join,merg ...

  7. 【GPU编解码】GPU硬解码---CUVID

    问题描述:项目中,需要对高清监控视频分析处理,经测试,其解码过程所占CPU资源较多,导致整个系统处理效率不高,解码成为系统的瓶颈. 解决思路: 利用GPU解码高清视频,降低解码所占用CPU资源,加速解 ...

  8. play HTTP路由 http://play-framework.herokuapp.com/zh/routes#syntax

    HTTP路由 HTTP路由(译者注:Play的路径映射机制)组件负责将HTTP请求交给对应的action(一个控制器Controller的公共静态方法)处理. 对于MVC框架来说,一个HTTP请求可以 ...

  9. jQuery源码分析-01总体架构

    1. 总体架构 1.1自调用匿名函数 self-invoking anonymous function 打开jQuery源码,首先你会看到这样的代码结构: (function( window, und ...

  10. How does Web Analytics works under sharePoint 2010

    [http://gokanx.wordpress.com/2013/06/15/how-does-web-analytics-works-under-sharepoint-2010/] You nee ...