POJ 1636 Prison rearrangement DFS+0/1背包

2024-06-08 10:48

本文主要是介绍POJ 1636 Prison rearrangement DFS+0/1背包,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目链接: POJ 1636 Prison rearrangement


Prison rearrangement
Time Limit: 3000MS Memory Limit: 10000K
Total Submissions: 2194 Accepted: 984

Description

In order to lower the risk of riots and escape attempts, the boards of two nearby prisons of equal prisoner capacity, have decided to rearrange their prisoners among themselves. They want to exchange half of the prisoners of one prison, for half of the prisoners 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

On the first line of the input is a single positive integer n, telling the number of test scenarios to follow. Each scenario begins with a line containing two non-negative integers m and r, 1 < m < 200 being the number of prisoners in each of the two prisons, 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

For each test scenario, output one line containing the largest integer k <= m/2 , such that it is possible to exchange k prisoners of the first prison for k prisoners of the second prison without getting two prisoners of any dangerous pair in the same prison.

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

Northwestern Europe 2003

题意:

       有两个监狱都有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]++;elsep2[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背包的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1041970

相关文章

poj2576(二维背包)

题意:n个人分成两组,两组人数只差小于1 , 并且体重只差最小 对于人数要求恰好装满,对于体重要求尽量多,一开始没做出来,看了下解题,按照自己的感觉写,然后a了 状态转移方程:dp[i][j] = max(dp[i][j],dp[i-1][j-c[k]]+c[k]);其中i表示人数,j表示背包容量,k表示输入的体重的 代码如下: #include<iostream>#include<

hdu2159(二维背包)

这是我的第一道二维背包题,没想到自己一下子就A了,但是代码写的比较乱,下面的代码是我有重新修改的 状态转移:dp[i][j] = max(dp[i][j], dp[i-1][j-c[z]]+v[z]); 其中dp[i][j]表示,打了i个怪物,消耗j的耐力值,所得到的最大经验值 代码如下: #include<iostream>#include<algorithm>#include<

csu(背包的变形题)

题目链接 这是一道背包的变形题目。好题呀 题意:给n个怪物,m个人,每个人的魔法消耗和魔法伤害不同,求打死所有怪物所需的魔法 #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>//#include<u>#include<map

hdu1011(背包树形DP)

没有完全理解这题, m个人,攻打一个map,map的入口是1,在攻打某个结点之前要先攻打其他一个结点 dp[i][j]表示m个人攻打以第i个结点为根节点的子树得到的最优解 状态转移dp[i][ j ] = max(dp[i][j], dp[i][k]+dp[t][j-k]),其中t是i结点的子节点 代码如下: #include<iostream>#include<algorithm

hdu1171(母函数或多重背包)

题意:把物品分成两份,使得价值最接近 可以用背包,或者是母函数来解,母函数(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v) 其中指数为价值,每一项的数目为(该物品数+1)个 代码如下: #include<iostream>#include<algorithm>

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n