POJ 1636 Prison rearrangement DFS+0/1背包
题目链接: id=1636">POJ 1636 Prison rearrangement
| Time Limit: 3000MS | Memory Limit: 10000K | |
| Total Submissions: 2194 | Accepted: 984 |
Description
of the other. However, from the archived information of the prisoners' crime history, they know that some pairs of prisoners are dangerous to keep in the same prison, and that is why they are separated today, i.e. for every such pair of prisoners, one prisoners
serves time in the first prison, and the other in the second one. The boards agree on the importance of keeping these pairs split between the prisons, which makes their rearrangement task a bit tricky. In fact, they soon find out that sometimes it is impossible
to fulfil their wish of swapping half of the prisoners. Whenever this is the case, they have to settle for exchanging as close to one half of the prisoners as possible.
Input
and r the number of dangerous pairs among the prisoners. Then follow r lines each containing a pair xi yi of integers in the range 1 to m,which means that prisoner xi of the first prison must not be placed in the same prison as prisoner yi of the second prison.
Output
Sample Input
3
101 0
3 3
1 2
1 3
1 1
8 12
1 1
1 2
1 3
1 4
2 5
3 5
4 5
5 5
6 6
7 6
8 7
8 8
Sample Output
50
0
3
Source
题意:
有两个监狱都有m个囚犯,如今为了更好地管理,须要相互交换一部分人(小于等于m/2)。可是有一个问题就是,某一些有冲突的人的人不能待在一个监狱里面。也就是说。监狱1中的囚犯A和监狱2的囚犯B假设放在一起的话,将会产生非常严重的后果。如今你要求出可行的最大交换人数(不大于一半)。使得有冲突的人不能待在一个监狱里面。
思路:
这道题卡了挺久,网上看了非常多题解,然后自己认真想了想,发觉自己对背包的理解还不是非常透彻。
想想看,首先要将有关系的人分开成一个个集合(p, q)。表示监狱1中要拿出p个人交换的话,监狱2得同一时候拿出q个人。怎样找到这个集合呢?细心地人一眼就看出了是求连通分量了。
先构图,由于两个监狱的囚犯的人数和编号都是一样的,为了构图方便。我们将第二个监狱的囚犯的便后向后挪m(+m)。
然后有关系的两个人之间连一条无向边。之后的事情就交给dfs求连通分支了,当然中间要记录监狱1和监狱2须要交换多少个人。
那如今我们得到了cnt个连通分支了,也就是有cnt个集合(p, q),如今看看0/1背包的思想。
我们用二维数组来记录在不发生危急的情况下可进行交换的情况。dp[i][j] = true表示第一个监狱拿出i个人、第二个监狱拿出j个人进行交换不产生冲突的情况。
利用滚动数组的思想,从后往前更新全部可能情况。
由于最后要保证两个监狱交换人数同样。因而找到最大的i使dp[i][i]
== true,i(《= m/2)就是我们要求的结果。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; int m, g[402][402];
int p1[202], p2[202], dp[110][110];
int cnt;
bool vis[402];
void dfs(int s)
{
vis[s] = true;
if(s <= m)
p1[cnt]++;
else
p2[cnt]++;
for(int i = 1; i <= 2*m; i++)
if(!vis[i] && g[s][i])
dfs(i);
}
int main()
{
int t, r, a, b;
//freopen("out.txt", "w", stdout);
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &m, &r);
memset(g, 0, sizeof(g));
while(r--)
{
scanf("%d%d", &a, &b);
g[a][b+m] = g[b+m][a] = 1;
}
memset(vis, false, sizeof(vis));
cnt = 0;
for(int i = 1; i <= 2*m; i++)
if(!vis[i])
{
p1[cnt]= p2[cnt] = 0;
dfs(i);
cnt++;
}
memset(dp, false, sizeof(dp));
dp[0][0] = true;
for(int i = 0; i < cnt; i++)
for(int j = m/2; j >= p1[i]; j--)
for(int k = m/2; k >= p2[i]; k--)
if(dp[j-p1[i]][k-p2[i]])
dp[j][k] = true;
int index = m/2;
for(int i = m/2; i >= 0; i--)
if(dp[i][i])
{
index = i;
break;
}
printf("%d\n", index);
}
return 0;
}
POJ 1636 Prison rearrangement DFS+0/1背包的更多相关文章
- poj 1636 Prison rearrangement
Prison rearrangement Time Limit: 3000MS Memory Limit: 10000K Total Submissions: 2158 Accepted: ...
- POJ 1745 【0/1 背包】
题目链接:http://poj.org/problem?id=1745 Divisibility Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- [POJ]Find The Multiple(DFS)
题目链接 http://poj.org/problem?id=1426 题意 输入一个数n,输出任意一个 只含0.1且能被n整除的数m.保证n<=200,m最多100位. 题解 DFS/BFS都 ...
- poj 1011 Sticks (DFS+剪枝)
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 127771 Accepted: 29926 Descrip ...
- POJ 1753 Flip Game (DFS + 枚举)
题目:http://poj.org/problem?id=1753 这个题在開始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每一个棋子翻一遍.然后顺利的过了.所以也就没有深究. 省赛前 ...
- Sticks POJ - 1011 少林神棍 dfs四次剪枝
http://poj.org/problem?id=1011 题意:若干根棍子被截成小段的木棒,现在给你这些木棒,问最短可以拼出的棍子长度. 题解:搜索,dfs(r,m) 二个参数分别代表还剩r个木棒 ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- POJ 1753 Flip Game DFS枚举
看题传送门:http://poj.org/problem?id=1753 DFS枚举的应用. 基本上是参考大神的.... 学习学习.. #include<cstdio> #include& ...
- POJ 1321 棋盘问题 --- DFS
POJ 1321 题目大意:给定一棋盘,在其棋盘区域放置棋子,需保证每行每列都只有一颗棋子. (注意 .不可放 #可放) 解题思路:利用DFS,从第一行开始依次往下遍历,列是否已经放置棋子用一个数组标 ...
随机推荐
- Calling a PL/SQL procedure in ODI
新建procedure 新建一个测试表格EMP: CREATE TABLE EMP ( ) CONSTRAINT PK_EMP PRIMARY KEY, ENAME ), JOB ), MGR ), ...
- iOS变量定义在 .h 还是 .m 中
前言 曾经我定义变量一直都是定义在.h文件里.后来看别人的代码,发现非常多人都把一些变量定 义在.m文件里. 后来我自己试了一把,发现变量既能够定义在.h文件里也能够定义在.m文 件中,尽管这是个非常 ...
- V-rep学习笔记:机器人模型创建4—定义模型
完成之前的操作后终于来到最后一步——定义模型,即将之前创建的几何体.关节等元素按层级关系组织成为一个整体. 将最后一个连杆robot_link_dyn6拖放到相应的关节(robot_joint6)下, ...
- 读书笔记——spring cloud 中 HystrixCommand的四种执行方式简述
读了<Spring Cloud 微服务实战>第151-154页, 总结如下: Hystrix存在两种Command,一种是HystrixCommand,另一种是HystrixObserva ...
- DUBBO本地搭建及小案例 (转)
DUBBO的介绍部分我这里就不介绍了,大家可参考官方文档. DUBBO的注册中心安装 DUBBO的注册中心支持好几种,公司用到zookeeper注册中心,所以我这边只说明zookeeper注册中心如何 ...
- Java Web(5) Spring 下使用Junit4 单元测试
1. 如何在不启动Tomcat服务器的情况下对,Dao这些不依赖使用Servlet API的类来进行单元测试呢? 其实在Spring框架体系中,已经有一套自己的测试代码,其中就是依赖使用Junit来进 ...
- ubuntu中查看各种设备和资源的命令汇总
一.系统信息 1.查看内核信息: $uname -a 2.查看操作系统版本: ...
- Rot13加密算法
Rot13是一种非常简单的替换加密算法,只能加密26个英语字母.方法是:把每个字母用其后第13个字母代替. 因为有26个字母,取其一半13. s = "xrlvf23xfqwsxsqf&qu ...
- vim缩进参考线
编辑缩进嵌套的文件时想找到对应的层级比较困难,写了一个函数,使用cc选项设定一条辅助线,标识到指定的缩进层级.代码如下: ? ReferenceLine 1 2 3 4 5 6 7 8 9 10 11 ...
- 你应该学会使用的5个ruby方法
今天看到了这篇文章--Five Ruby Methods You Should Be Using,感觉收获颇丰,先简单翻译一下先. 作者写这篇文章的契机是在Exercism上看到了很多ruby代码可以 ...