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

【poj1129】Channel Allocation

时间:2016-06-12 20:25:23      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

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. 

Source

题解

题意,给定一个N个节点的无向图,求颜色最少的染色方案使相邻点的颜色不同

由于n<27,可以直接搜索

记下相邻的节点,从小到大枚举颜色,当相邻节点用过的时候,这个颜色就不能再用。

#include<iostream>
#include<cstdio>
using namespace std;
struct nod
{
    int t[27],s;
}node[27];
int n;
int main()
{
    while (cin>>n)
    {
        if (!n) break;
        getchar();
        for (int i=1;i<=n;i++)
        {
            char ch;
            ch=getchar();
            getchar();
            node[i].s = 0;
            while ((ch = getchar() )!=\n)
            {
                int j = ch - A+1;
                node[i].t[++node[i].s] = j;
            }
        }
        int color[27] = {0};
        color[1] = 1;//节点i的颜色 
        int sumcolor = 1;
        for (int i=2;i<=n;i++)
        {
            bool vis[27] = {false};
            color[i] =i + 1;
            for (int j = 1;j<=node[i].s;j++)//枚举后继,后继已经染色,那么这种颜色不可用 
                if (color[j])
                    vis[color[j]] = true;
            for (int k=1;k<=sumcolor+1;k++)
                if (!vis[k] && k<color[i])
                {
                    color[i] = k;
                    break;
                }
            if (color[i]>sumcolor)    sumcolor = color[i];
        }
        printf("%d ",sumcolor);
        if (sumcolor>1) printf("channels needed.\n");
            else printf("channel needed.\n");
    }
}

 

【poj1129】Channel Allocation

标签:

原文地址:http://www.cnblogs.com/liumengyue/p/5578626.html

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