zoukankan      html  css  js  c++  java
  • 迭代加深搜索 POJ 1129 Channel Allocation

    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. 
     1 /*-------------超时代码---------------*/
     2 /*
     3 一开始我直接用的dfs没有剪枝,就是dfs每一个点,枚举每一个频道,找到不相邻,就向下dfs,再加上回溯,每次复杂度是n^3,再加上题目询问的数据量有点大,就超时了。*/
     4 /*--------------------------*/
     5 #include<iostream>
     6 using namespace std;
     7 #include<cstdio>
     8 #include<cstring>
     9 #define N 30
    10 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.
    ",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<iostream>
     5 using namespace std;
     6 #include<cstdio>
     7 #include<cstring>
     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.
    ",sum);
    85         else printf("%d channel needed.
    ",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 }
  • 相关阅读:
    【idea-部署web项目】
    【IDEA下使用tomcat部署web项目】
    【 PLSQL Developer安装、tnsnames.ora配置 解答】
    【idea--git】
    【Sping管理bean的原理】
    【关于eclipse的一些自己常用的插件】
    【Spring-任务调度】
    【mysql存储引擎】
    【mysql-索引+存储过程+函数+触发器-更新。。。】
    【转-mysql-explain介绍】
  • 原文地址:https://www.cnblogs.com/c1299401227/p/5578779.html
Copyright © 2011-2022 走看看