zoukankan      html  css  js  c++  java
  • poj 1129 Channel Allocation (四色定理)

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

    题意:

    当一个广播电台在一个非常大的地区,广播站会用中继器来转播信号以使得每一个接收器都能接收到一个强烈的信号。然而,每个中继器必须慎重选择使用,使相邻的中继器不互相干扰。如果相邻的中继器使用不同的频道,那么就不会相互干扰。

    由于无线电频道是一有限的,一个给定的网络所需的中继频道数目应减至最低。编写一个程序,读取一个中继网络,然后求出需要的最低的不同频道数。

    View Code
     1 #include <iostream>
     2 #include<cstdio>
     3 using namespace std;
     4 struct node
     5 {
     6     int next[27];//后继
     7     int num;//后继的个数
     8 }point[27];
     9 int main()
    10 {
    11     int i,j,n;
    12     while(scanf("%d",&n)!=EOF)
    13     {
    14         if(n==0)
    15         break;
    16         getchar();
    17         for(i=1;i<=n;i++)
    18         {
    19             getchar();//节点序号
    20             getchar();//冒号
    21             char c;
    22             point[i].num=1;
    23             while(scanf("%c",&c),c!='\n')//输入序号的后继
    24             {
    25                 int u;
    26                 u=c-'A'+1;
    27                 point[i].next[point[i].num]=u;
    28                 point[i].num++;
    29             }
    30         }
    31         //从最小开始染色
    32         int color[27]={0};//标记第i个点染的最小颜色
    33         color[1]=1;
    34         int max=1;//最大染色数
    35         for(i=1;i<=n;i++)
    36         {
    37             color[i]=n+1;//假设染最大的颜色
    38             int vis[27]={0};//标记
    39             for(j=1;j<point[i].num;j++)//枚举后继各个点
    40             {
    41                 if(color[point[i].next[j]])//如果某个后继已染色则标记
    42                 vis[color[point[i].next[j]]]=1;
    43             }
    44             for(j=1;j<=n;j++)//枚举每种颜色,从最小开始,可能会暂时出现大于4的颜色,不能枚举4个颜色
    45             {
    46                 if(!vis[j]&&color[i]>j)
    47                 {
    48                     color[i]=j;//更新i节点的颜色,使之最小
    49                     break;
    50                 }
    51             }
    52             if(max<color[i])
    53             {
    54                 max=color[i];
    55                 if(max==4)//由四色定理可知,等于4的时候就不需要再继续下去了,剪枝
    56                 break;
    57             }
    58         }
    59         if(max==1)
    60         {
    61             printf("%d channel needed.\n",max);
    62         }
    63         else
    64         printf("%d channels needed.\n",max);
    65     }
    66     return 0;
    67 }
  • 相关阅读:
    约数的问题
    广度搜索基本逻辑
    奇葩概念
    一枚前端UI组件库 KUI for React
    一枚前端UI组件库 KUI for Vue
    跨域的常见问题和解决方案
    Comet,SSE,WebSocket前后端的实现
    web渐进式应用PWA
    IIS 部署node
    javascript 时间戳
  • 原文地址:https://www.cnblogs.com/wanglin2011/p/2916469.html
Copyright © 2011-2022 走看看