题目链接:poj1129 Channel Allocation

题意:要求相邻中继器必须使用不同的频道,求需要使用的频道的最少数目。

题解:就是求图的色数,这里采用求图的色数的近似有效算法——顺序着色算法(实质是一种贪心策略:在给任何一个顶点着色时,采用其邻接顶点中没有使用的,编号最小的颜色)。

注:中继器网络是一个平面图,即图中不存在相交的边。

看讨论后发现这组数据,AC代码没过orz:

6

A:BEF

B:AC

C:BD

D:CEF

E:ADF

F:ADE 正确答案应该是3 : A(1)B(2)C(3)D(1)E(2)F(3)但是AC的代码得出了错误答案4

           数据弱啊ORZ。。。。。。

顺序着色算法(有问题的AC代码,毕竟这是求近似解,不能保证解最优):

 #include<cstdio>
#include<cmath>
#include<queue>
#include<cstring>
#include<algorithm>
#define CLR(a,b) memset((a),(b),sizeof((a)))
using namespace std; const int N = ; char s[N];
int g[N][N];
int n, ans;
bool vis[N]; //每种颜色的使用标记
int color[N]; //各顶点的颜色
void greedy(){
int i, j;
for(i = ; i < n; ++i){
CLR(vis,);
for(j = ; j < n; ++j){
if(g[i][j] && color[j] != -){//邻接顶点j已经着色
vis[color[j]] = ;
}
}
for(j = ; j <= i; ++j){
//j为顶点i的所有邻接顶点中没有使用的,编号最小的颜色
if(!vis[j])break;
}
color[i] = j;//给i着色第j种颜色
}
for(i = ; i < n; ++i)
if(color[i] > ans)
ans = color[i];
ans++;
}
int main(){
int i, j, l;
while(~scanf("%d", &n),n){
CLR(g,);
ans = ;
for(i = ; i < n; ++i)
color[i] = -;
for(i = ; i < n; ++i){
scanf("%s", s);
l = strlen(s) - ; //与第i个中继器相邻的中继器数目
for(j = ; j < l; ++j){
g[i][s[j+]-'A'] = ;
g[s[j+]-'A'][i] = ;
}
}
greedy();
if(ans == )
printf("%d channel needed.\n", ans);
else
printf("%d channels needed.\n", ans);
}
return ;
}

在网上搜了一下,找到个正确的AC代码,用四色定理加深搜可解:

 #include<cstdio>
#include<cstring>
#include<algorithm>
#define CLR(a,b) memset((a),(b),sizeof((a)))
using namespace std;
const int N = ; char s[N];
int n, ans;
int color[N];
bool g[N][N];
bool jud(int x, int c){
for(int i = ; i < n; ++i)
if(i != x && g[x][i] && color[i] == c)
return ;
return ;
}
void dfs(int x){
if(x == n){
ans = min(ans, *max_element(color, color + n));
return;
}
for(int i = x; i < n; ++i){
for(int j = ; j <= ; ++j){
if(jud(i, j)){
color[i] = j;
dfs(i + );
}
}
}
}
int main(){
int i, j, l;
while(~scanf("%d",&n),n){
CLR(g,);
CLR(color,);
color[] = ;
ans = ;
for(i = ; i < n; ++i){
scanf("%s", s);
l = strlen(s) - ;
for(j = ; j < l; ++j){
g[i][s[j+]-'A'] = ;
g[s[j+]-'A'][i] = ;
}
}
dfs();
if(ans == )
printf("%d channel needed.\n", ans);
else
printf("%d channels needed.\n", ans);
}
return ;
}

poj1129 Channel Allocation(染色问题)的更多相关文章

  1. poj1129 Channel Allocation

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14361   Accepted: 73 ...

  2. 快速切题 poj1129 Channel Allocation

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12334   Accepted: 63 ...

  3. PKU 1129 Channel Allocation(染色问题||搜索+剪枝)

    题目大意建模: 一个有N个节点的无向图,要求对每个节点进行染色,使得相邻两个节点颜色都不同,问最少需要多少种颜色? 那么题目就变成了一个经典的图的染色问题 例如:N=7 A:BCDEFG B:ACDE ...

  4. POJ-1129 Channel Allocation (DFS)

    Description When a radio station is broadcasting over a very large area, repeaters are used to retra ...

  5. POJ 1129 Channel Allocation(DFS)

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13173   Accepted: 67 ...

  6. POJ 1129:Channel Allocation 四色定理+暴力搜索

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13357   Accepted: 68 ...

  7. 迭代加深搜索 POJ 1129 Channel Allocation

    POJ 1129 Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14191   Acc ...

  8. Channel Allocation

    Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13231 Accepted: 6774 D ...

  9. Channel Allocation (poj 1129 dfs)

    Language: Default Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12 ...

随机推荐

  1. Python命令行解析argparse常用语法使用简介

    查看原文:http://www.sijitao.net/2000.html python中的命令行解析最简单最原始的方法是使用sys.argv来实现,更高级的可以使用argparse这个模块.argp ...

  2. 多命令顺序执行、管道符 ; && || |

    多命令顺序执行:

  3. .NET中的三种接口实现方式

    摘自:http://www.cnblogs.com/zhangronghua/archive/2009/11/25/1610713.html 一般来说.NET提供了三种不同的接口实现方式,分别为隐式接 ...

  4. 概念学习(Concept Learning)

    从特殊的训练样例中归纳出一般函数是机器学习的核心问题.一般函数是对理想目标函数的函数逼近(function approximation).简而言之,从特殊到普通.与此对应的是演绎推理(deductiv ...

  5. 基于jQuery的移动轮播图(支持触屏)

    移动轮播图我看到两款, 一款是无线天猫的m.tmall.com,实现了无缝轮播. 一款是蘑菇街的,没有实现无缝轮播. 我自己重写一个,类似蘑菇街 <!doctype html> <h ...

  6. 团队作业Week5之团队贡献分的分配

    一.团队贡献分的分配规则 首先,我们团队共有5个人,平均每个人50分,所以我们团队的总分为5*50=250,我们先把50分分成以下几份: 序号 贡献类型 权重 分数 1 代码贡献 40% 20 * 5 ...

  7. java SE (java Standard Edition)

    14.10.22 学习java SE的Object: -------------------------------------15.11.18----

  8. Spring集成JPA提示Not an managed type

    在做Spring与JPA集成时,出现问题如下: Caused by: java.lang.IllegalArgumentException: Not an managed type: class co ...

  9. oninput,onpropertychange,onchange的用法和区别【转载】

    1.前言 由于工作需要,需实现一个类似于微博输入框的功能,在用户动态输入文字的时候,修改提示“您还可以输入XX字”.如下图所示: 因此,稍微研究了一下oninput,onpropertychange, ...

  10. 闹钟--alarmManager

    1.AlarmManager,顾名思义,就是“提醒”,是Android中 常用的一种系统级别的提示服务,在特定的时刻为我们广播一个指定的Intent.简单的说就是我们设定一个时间,然后在该时间到来 时 ...