博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
迭代加深搜索 POJ 1129 Channel Allocation
阅读量:4704 次
发布时间:2019-06-09

本文共 6405 字,大约阅读时间需要 21 分钟。

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

2A:B:4A:BCB:ACDC:ABDD:BC4A:BCDB:ACDC:ABDD:ABC0

Sample Output

1 channel needed.3 channels needed.4 channels needed.
1 /*-------------超时代码---------------*/ 2 /* 3 一开始我直接用的dfs没有剪枝,就是dfs每一个点,枚举每一个频道,找到不相邻,就向下dfs,再加上回溯,每次复杂度是n^3,再加上题目询问的数据量有点大,就超时了。*/ 4 /*--------------------------*/ 5 #include
6 using namespace std; 7 #include
8 #include
9 #define N 3010 struct Edge{11 int v,last;12 }edge[N*N];13 int head[N];14 int sum=(1<<31)-1,n,t=0;15 int flag[N],pd[N];16 bool bb=false;17 inline void add_edge(int u,int v)18 {19 ++t;20 edge[t].v=v;21 edge[t].last=head[u];22 head[u]=t;23 }24 inline void input()25 {26 char s[N];27 for(int i=1;i<=n;++i)28 {29 scanf("%s",s+1);30 int len=strlen(s+1);31 for(int j=3;j<=len;++j)32 add_edge(s[1]-'A'+1,s[j]-'A'+1);33 }34 }35 inline void dfs(int k)36 {37 if(k==n+1)38 {39 int ans=0;40 for(int j=1;j<=n;++j)41 if(flag[j]) ans++;42 sum=min(ans,sum);43 return;44 }45 for(int i=1;i<=n;++i)46 {47 int biaozhi=true;48 for(int l=head[k];l;l=edge[l].last)49 {50 if(pd[edge[l].v]==i)51 {52 biaozhi=false;53 break;54 }55 }56 if(!biaozhi) continue;57 pd[k]=i;58 flag[i]++;59 dfs(k+1);60 flag[i]--;61 pd[k]=0;62 }63 64 }65 int main()66 {67 while(scanf("%d",&n)==1)68 {69 if(n==0) break;70 input();71 dfs(1);72 printf("%d channels needed.\n",sum);73 memset(edge,0,sizeof(edge));74 memset(head,0,sizeof(head));75 sum=(1<<31)-1;t=0;bb=false;76 memset(flag,0,sizeof(flag));77 }78 return 0;79 }
1 /*-------------对于上面那个代码-----------------*/ 2 特殊数据: 3 9 4 A:B 5 B: 6 C: 7 D: 8 E: 9 F:10 G:11 H:12 I:13 用上面的代码来处理这个非常稀疏的图时间是很长的,因为for(i-->n)枚举频道中的if语句几乎始终成立,那么dfs的复杂度就到了n^n的增长速度,当n==8时,已经1.6*10^8多了,自然会超时,所以必须改为迭代加深搜索。限定搜索的深度,实际上是不会到n的

 

1 /*改成迭代加深搜索之后,速度果然快了许多。 2 还有一个值得注意的地方:当sum是1的时候,channel是单数形式,其他时候是复数形式(英语不好被坑了) 3 */ 4 #include
5 using namespace std; 6 #include
7 #include
8 #define N 30 9 struct Edge{10 int v,last;11 }edge[N*N];12 int head[N];13 bool vis[N][N]={
false};14 int sum=(1<<31)-1,n,t=0;15 int pd[N];16 bool flag=false;17 inline void add_edge(int u,int v)18 {19 if(vis[u][v]||vis[v][u]) return ;20 vis[u][v]=true;vis[v][u]=true;21 ++t;22 edge[t].v=v;23 edge[t].last=head[u];24 head[u]=t;25 ++t;26 edge[t].v=u;27 edge[t].last=head[v];28 head[v]=t;29 }30 inline void input()31 {32 char s[N];33 for(int i=1;i<=n;++i)34 {35 scanf("%s",s+1);36 int len=strlen(s+1);37 for(int j=3;j<=len;++j)38 add_edge(s[1]-'A'+1,s[j]-'A'+1);39 }40 }41 inline void dfs(int k,int minn)42 {43 if(k==n+1)44 {45 flag=true;46 return;47 }48 for(int i=1;i<=minn;++i)49 {50 bool biaozhi=true;51 for(int l=head[k];l;l=edge[l].last)52 {53 if(pd[edge[l].v]==i)54 {55 biaozhi=false;56 break;57 }58 }59 if(!biaozhi) continue;60 pd[k]=i;61 dfs(k+1,minn);62 if(flag) return;63 pd[k]=0;64 }65 66 }67 int main()68 {69 while(scanf("%d",&n)==1)70 {71 if(n==0) break;72 input();73 for(int i=1;i<=n;++i)74 {75 dfs(1,i);76 if(flag)77 {78 sum=i;79 memset(pd,0,sizeof(pd));80 break;81 }else memset(pd,0,sizeof(pd));82 }83 if(sum>1)84 printf("%d channels needed.\n",sum);85 else printf("%d channel needed.\n",sum);86 memset(edge,0,sizeof(edge));87 memset(head,0,sizeof(head));88 sum=(1<<31)-1;t=0;89 memset(vis,false,sizeof(vis));90 flag=false;91 }92 return 0;93 }

 

转载于:https://www.cnblogs.com/c1299401227/p/5578779.html

你可能感兴趣的文章
(14)嵌入式软件开发工程师技能要求总结
查看>>
[hackerrank]Closest Number
查看>>
volatile关键字
查看>>
[Android] TabLayout设置下划线(Indicator)宽度
查看>>
<潭州教育>-Python学习笔记@条件与循环
查看>>
web自动化之验证码识别解决方案
查看>>
netty接收大文件的方法
查看>>
软件工程设计之四则运算
查看>>
SpringMVC @ResponseBody 406
查看>>
HDOJ---2824 The Euler function[欧拉函数]
查看>>
KMP算法
查看>>
Atlas学习之开始篇[转]
查看>>
第二章 在HTML页面里使用javaScript
查看>>
【Educational Codeforces Round 48 (Rated for Div. 2) D】Vasya And The Matrix
查看>>
正则表达式的性能评测
查看>>
CF1172B Nauuo and Circle
查看>>
CF1178D Prime Graph
查看>>
CF1190D Tokitsukaze and Strange Rectangle
查看>>
CF1202F You Are Given Some Letters...
查看>>
CF1179C Serge and Dining Room
查看>>