码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1129 Channel Allocation

时间:2015-08-12 18:47:48      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

          poj 1129 Channel Allocation

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. 


 题意:在连通无向图中给各个顶点染色,要求相邻的顶点要有不同的颜色。求所用最少的颜色数。我自己没看懂。。。
tip:dfs(我还不是很熟悉)



  1 #include <iostream>
  2 #include <stdio.h>
  3 #include <string.h>
  4 
  5 using namespace std;
  6 
  7 int N;
  8 bool num[30][30];
  9 int flag[30];
 10 int minS;
 11 bool mark;
 12 
 13 bool judge(int x,int value)///判断是否相邻的颜色相同
 14 {
 15     int i;
 16     for(i=1;i<=N;i++)
 17     {
 18         if(num[x][i]&&flag[i]==value&&i!=x)///注意i!=x
 19         {
 20             return false;
 21         }
 22     }
 23     return true;
 24 }
 25 
 26 void dfs(int step)
 27 {
 28 
 29     if(step==N+1)///深搜到最后
 30     {
 31         int k;
 32         int mm=0;
 33         for(k=1;k<=N;k++)
 34         {
 35             if(flag[k]>mm)///几种颜色
 36             {
 37                 mm=flag[k];
 38             }
 39         }
 40         minS=mm;
 41         mark=true;
 42         return;
 43     }
 44 
 45     if(mark)
 46     {
 47         return;
 48     }
 49 
 50     int i;
 51     for(i=1;i<=N;i++)
 52     {
 53         flag[step]=i;
 54         if(judge(step,i)==false)
 55         {
 56             flag[step]=0;
 57             continue;
 58         }
 59         dfs(step+1);
 60         if(mark)
 61             return;
 62         flag[step]=0;
 63     }
 64 
 65 }
 66 int main()
 67 {
 68 
 69     while(~scanf("%d",&N) && N )
 70     {
 71         char str[50];
 72         int i,j;
 73         memset(num,false,sizeof(num));
 74 
 75         for(i=0;i<N;i++)///用以标记是否相邻
 76         {
 77             scanf("%s",str);
 78             for(j=2;str[j]!=\0;j++)
 79             {
 80                 int a=str[j]-A+1;
 81                 num[i+1][a]=true;
 82             }
 83         }
 84 
 85         memset(flag,0,sizeof(flag));///记为颜色
 86         flag[1]=1;
 87         mark=false;
 88         dfs(2);///深搜
 89 
 90         if(minS==1)
 91         {
 92             printf("%d channel needed.\n",minS);
 93         }
 94         else
 95         {
 96             printf("%d channels needed.\n",minS);
 97         }
 98 
 99     }
100     return 0;
101 }

 

poj 1129 Channel Allocation

标签:

原文地址:http://www.cnblogs.com/moqitianliang/p/4725008.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!