poj_1129 Channel Allocation

2 篇文章 0 订阅

Channel Allocation

Time Limit: 1000MS

Memory Limit: 10000K

Total Submissions: 9669

Accepted: 4919

题目链接:http://poj.org/problem?id=1129

Description

When a radio station is broadcasting over a very largearea, repeaters are used to retransmit the signal so that every receiver has astrong signal. However, the channels used by each repeater must be carefullychosen so that nearby repeaters do not interfere with one another. Thiscondition is satisfied if adjacent repeaters use different channels.

Since the radio frequency spectrum is a precious resource, the number ofchannels required by a given network of repeaters should be minimised. You haveto write a program that reads in a description of a repeater network anddetermines the minimum number of channels required.

Input

The input consists of a number of maps of repeaternetworks. Each map begins with a line containing the number of repeaters. Thisis between 1 and 26, and the repeaters are referred to by consecutiveupper-case letters of the alphabet starting with A. For example, ten repeaterswould have the names A,B,C,...,I and J. A network with zero repeaters indicatesthe end of input.

Following the number of repeaters is a list of adjacency relationships. Eachline has the form:

A:BCDH

which indicates that the repeaters B, C, D and H are adjacent to the repeaterA. The first line describes those adjacent to repeater A, the second thoseadjacent to B, and so on for all of the repeaters. If a repeater is notadjacent 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 linesegments 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 noadjacent channels interfere. The sample output shows the format of this line.Take care that channels is in the singular form when only one channel isrequired.

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

Southern African2001

 

解题思路:

         这是一个典型的图的染色问题,根据输入建立邻接矩阵图,判断最少几种颜色可以将所有点染色,每两个点颜色必须不一样。这种问题解题是这样的:先建图,然后遍历图的每一个节点,每个节点默认的颜色是第一种颜色,判断这一点上了这一种颜色会不会和其他点有冲突,如果没有就上这种颜色,如果该颜色与其他点发生冲突,就给这一点上第二种颜色,在判断,依次循环,直到没有冲突后输出,n个节点的图最多需要上n中颜色。在求其中颜色中最大值就是题目要的答案。

代码:

#include <iostream>
#include<cstdio>
#include<cstring>
#define MAX 30
using namespace std;

int g[MAX][MAX];
int color[MAX];

//判断该染色是否符合
bool isOk(int k)
{
    for(int i=0;i<k;i++)
    {
        if(g[k][i]==1 && color[k]==color[i])//与k相邻的边有染相同颜色的点,返回false
            return false;
    }
    return true;
}
//对图g进行着色
int graphColor(int n)
{
    int sum=0;
    memset(color,0,sizeof(color));
    while(sum>=0)
    {
        color[sum]=color[sum]+1;
        while(color[sum]<n)
        {//染色总数不操作顶点总数
            if(isOk(sum))
                break;
            else//如果不符合,就染别的颜色
                color[sum]=color[sum]+1;
        }
		//全部已经染色,输出
        if(sum==n-1 && color[sum]<=n)
        {
            int max=1;
            for(int i=0;i<n;i++)
            {
                if(max<color[i])
                {
                    max=color[i];
                }
			//	printf("%d\t",color[i]);
            }
            return max;
        }
        else if(color[sum]<n && sum<n)
            sum++;//处理下一个顶点
        else
        {
            color[sum]=0;
            sum--;
        }
    }
    return 1;
}

void createGraph()
{
    int n;

    while(true)
    {
        scanf("%d",&n);
        getchar();
        if(n==0)
            break;
        memset(g,0,sizeof(g));
        //建图——邻接矩阵,从0开始
        int t=n;
        while(n--)
        {
            char ch[MAX];
            gets(ch);
            int id1,id2;
            id1=ch[0]-'A';
            for(int i=2;ch[i]!='\0';i++)
            {
                id2=ch[i]-'A';
                g[id1][id2]=1;
            }
        }
        int sum = graphColor(t);
        if(sum==1)
            printf("1 channel needed.\n");
        else
            printf("%d channels needed.\n",sum);
    }
}

int main()
{
    createGraph();
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯的世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值