迭代加深搜索 POJ 1129 Channel Allocation
POJ 1129 Channel Allocation
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 14191 | Accepted: 7229 |
Description
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
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
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的更多相关文章
- POJ 1129 Channel Allocation(DFS)
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13173 Accepted: 67 ...
- POJ 1129 Channel Allocation DFS 回溯
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 15546 Accepted: 78 ...
- poj 1129 Channel Allocation ( dfs )
题目:http://poj.org/problem?id=1129 题意:求最小m,使平面图能染成m色,相邻两块不同色由四色定理可知顶点最多需要4种颜色即可.我们于是从1开始试到3即可. #inclu ...
- POJ 1129 Channel Allocation 四色定理dfs
题目: http://poj.org/problem?id=1129 开始没读懂题,看discuss的做法,都是循环枚举的,很麻烦.然后我就决定dfs,调试了半天终于0ms A了. #include ...
- poj 1129 Channel Allocation
http://poj.org/problem?id=1129 import java.util.*; import java.math.*; public class Main { public st ...
- 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 ...
- 迭代加深搜索POJ 3134 Power Calculus
题意:输入正整数n(1<=n<=1000),问最少需要几次乘除法可以从x得到x的n次方,计算过程中x的指数要求是正的. 题解:这道题,他的结果是由1经过n次加减得到的,所以最先想到的就是暴 ...
- POJ1129Channel Allocation[迭代加深搜索 四色定理]
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14601 Accepted: 74 ...
- poj 2248 Addition Chains (迭代加深搜索)
[题目描述] An addition chain for n is an integer sequence with the following four properties: a0 = 1 am ...
随机推荐
- JPA学习(6)JPQL
JPQL语言,即 Java Persistence Query Language 的简称.JPQL 是一种和 SQL 非常类似的中间性和对象化查询语言,它最终会被编译成针对不同底层数据库的 SQL 查 ...
- [PE结构分析] 8.输入表结构和输入地址表(IAT)
在 PE文件头的 IMAGE_OPTIONAL_HEADER 结构中的 DataDirectory(数据目录表) 的第二个成员就是指向输入表的.每个被链接进来的 DLL文件都分别对应一个 IMAGE_ ...
- bootstrap 分页
1.背景: 前端页面使用bootstrap分页,同时与搜索条件联动: 2. jsp页面由服务端返回后, 异步请求动态创建表格, 分页的数据由服务端第一次返回后初始化, 以后每次异步请求再更新. jsp ...
- (旧)子数涵数·DW——网页制作的流程
PS:这是我很早以前的一个废掉的项目. 当时用的还是table排版,现在基本都是div了吧. 这个项目前段时间,我还抢救过一次,后来还是放弃了. 先行.网页制作的流程分为哪些呢? 一.网站策划(当时, ...
- php学习笔记:读取文档的内容,利用php修改文档内容
直接上代码 <?php /** * Created by PhpStorm. * User: Administrator * Date: 2016/9/10 0010 * Time: 20:27 ...
- 使用PowerQuery操作OData数据
Excel是我们耳熟的办公软件.PowerQuery是一个允许连接多种数据源的Excel插件.它能从一个网页上智能查询数据.使用PowerQuery能合并数据集使用join,merg ...
- 【GPU编解码】GPU硬解码---CUVID
问题描述:项目中,需要对高清监控视频分析处理,经测试,其解码过程所占CPU资源较多,导致整个系统处理效率不高,解码成为系统的瓶颈. 解决思路: 利用GPU解码高清视频,降低解码所占用CPU资源,加速解 ...
- play HTTP路由 http://play-framework.herokuapp.com/zh/routes#syntax
HTTP路由 HTTP路由(译者注:Play的路径映射机制)组件负责将HTTP请求交给对应的action(一个控制器Controller的公共静态方法)处理. 对于MVC框架来说,一个HTTP请求可以 ...
- jQuery源码分析-01总体架构
1. 总体架构 1.1自调用匿名函数 self-invoking anonymous function 打开jQuery源码,首先你会看到这样的代码结构: (function( window, und ...
- 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 ...