A Za, A Za, Fighting...

坚信:勤能补拙

PKU 1129 Channel Allocation

问题:
http://acm.pku.edu.cn/JudgeOnline/problem?id=1129

思路:
好题,典型的图着色问题
首先,对于邻接关系,可以用二维数组来表示
具有邻接关系的节点不能使用同一种颜色,求所需颜色的最小值
深度优先搜索,当目前使用颜色个数已经超过当前最优解时进行减枝

代码:
 1 #define MAX_NUM 29
 2 #define INF 100000
 3 int graph[MAX_NUM][MAX_NUM];
 4 int color[MAX_NUM];
 5 int num, ans;
 6 
 7 void
 8 init()
 9 {
10     int i, j, len;
11     char conn[MAX_NUM];
12     memset(graph, 0sizeof(graph));
13     memset(color, -1sizeof(color));
14     ans = INF;
15     for(i=0; i<num; i++) {
16         scanf("%s", conn);
17         len = strlen(conn);
18         for(j=2; j<len; j++)
19             graph[i][conn[j]-'A'= 1;
20     }
21 }
22 
23 int
24 is_valid(int depth, int cindex)
25 {
26     int i;
27     for(i=0; i<depth; i++)
28         if(graph[depth][i] && color[i]==cindex)
29             return 0;
30     return 1;
31 }
32 
33 void 
34 dfs(int depth, int used_colors)
35 {
36     int i;
37     if(used_colors >= ans) /* pruning */
38         return;
39     if(depth == num) {
40         ans = used_colors<ans ? used_colors : ans;
41         return;
42     }
43     for(i=1; i<=used_colors; i++) {
44         if(is_valid(depth, i)) {
45             color[depth] = i;
46             dfs(depth+1, used_colors);
47             color[depth] = -1;
48         }
49     }
50     color[depth] = used_colors+1;
51     dfs(depth+1, used_colors+1);
52     color[depth] = -1;
53 }


posted on 2010-07-26 20:47 simplyzhao 阅读(200) 评论(0)  编辑 收藏 引用 所属分类: B_搜索


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2010年8月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234

统计

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜