poj1129 Channel Allocation(染色问题)
题目链接: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(染色问题)的更多相关文章
- poj1129 Channel Allocation
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14361 Accepted: 73 ...
- 快速切题 poj1129 Channel Allocation
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12334 Accepted: 63 ...
- PKU 1129 Channel Allocation(染色问题||搜索+剪枝)
题目大意建模: 一个有N个节点的无向图,要求对每个节点进行染色,使得相邻两个节点颜色都不同,问最少需要多少种颜色? 那么题目就变成了一个经典的图的染色问题 例如:N=7 A:BCDEFG B:ACDE ...
- POJ-1129 Channel Allocation (DFS)
Description When a radio station is broadcasting over a very large area, repeaters are used to retra ...
- POJ 1129 Channel Allocation(DFS)
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13173 Accepted: 67 ...
- POJ 1129:Channel Allocation 四色定理+暴力搜索
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13357 Accepted: 68 ...
- 迭代加深搜索 POJ 1129 Channel Allocation
POJ 1129 Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14191 Acc ...
- Channel Allocation
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13231 Accepted: 6774 D ...
- Channel Allocation (poj 1129 dfs)
Language: Default Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12 ...
随机推荐
- 【转】在网页中运行VB6程序
用VB6做的程序在网页里运行, 需要把程序做成OCX格式,下面简单做一介绍: 首先新建一个工程, 选择ActivX控件: 然后添加控件和代码: 然后F5运行 然后按下图设置,去掉弹出消息阻止 这样 ...
- Vmware10.0 安装系统以及使用笔记
1.安装教程参考 大致分为:vmware10.0安装-------建立虚拟机---------设置虚拟机---------启动虚拟机(IOS安装)---------安装系统---------安装vmt ...
- Messages消息处理
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- ServiceStack.OrmLite 笔记10-group having 分页orderby等
group having 分页等 var ev = OrmLiteConfig.DialectProvider.SqlExpression(); group的使用 同sql一样,注意group分组的字 ...
- JPG 批量压缩、 PNG32、PNG24转PNG 透明批量压缩工具 【JPNG】 支持多级目录
说在最前,压缩不一定是最好的,仅仅是为了方便自己工作需要.主要是手机端图片 算法说明:JPG压缩使用的是 adobe 的 JPGEncoder+ AIR的JPEGEncoderOptions (注 ...
- iOS - UIScrollView
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIScrollView : UIView <NSCoding> @available(iOS 2.0, ...
- Nginx模块学习之————accesskey权限模块使用(Nginx防盗链详细解说),防止别人下载文件和图片
nginx 的第三方模块ngx_http_accesskey_module 来实现下载文件的防盗链 1.具体安装教程:http://www.cnblogs.com/tinywan/p/5983694. ...
- Redis实践操作之—— keyspace notification(键空间通知)
一.需求分析: 设置了生存时间的Key,在过期时能不能有所提示? 如果能对过期Key有个监听,如何对过期Key进行一个回调处理? 如何使用 Redis 来实现定时任务? 二.序言: 本文所说的定时任务 ...
- php使用cURL实现Get和Post请求的方法
1.cURL介绍 cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP.FTP.TELNET等.最爽的是,PHP也支持 cURL 库.本文将介绍 cURL 的一些高级特性 ...
- CnPlugin 1.5.400
本软件CnPlugin是PL/SQL Developer工具插件,支持PL/SQL Developer 7.0以上版本.增加了PL/SQL Developer工具本身所没有的一些小功能,功能基本一些已 ...