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]++;
    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背包

时间: 2024-12-09 03:45:34

POJ 1636 Prison rearrangement DFS+0/1背包的相关文章

POJ 3628 Bookshelf 2 0/1背包和DFS两种解法

题目链接:POJ 3628 Bookshelf 2 Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7462   Accepted: 3436 Description Farmer John recently bought another bookshelf for the cow library, but the shelf is getting filled up quite quickly,

【POJ】3009 Curling 2.0 ——DFS

Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11432   Accepted: 4831 Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is

POJ 1745 【0/1 背包】

题目链接:http://poj.org/problem?id=1745 Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13431   Accepted: 4774 Description Consider an arbitrary sequence of integers. One can place + or - operators between integers in the sequen

Poj 1112 Rebuilding Roads(树形DP+背包)

题意:给你由N个点构成一颗树,问要孤立出一个有P个节点的子树最少需要删除多少条边.N的范围最大为150 N的范围不大,很容易想到在树上面做背包.把每个节点都看成一个背包,然后把每个儿子节点都看成是一组物品.为什么是一组呢,那是因为假设以儿子为根的节点的子树有S个节点,那么就有S+1种情况,要么将这整棵子树舍弃,要么从这个子树中取1-S个节点. 设f[i][j]为以i为根节点的子树,孤立出以i为根节点,一共含有j个节点的子树最少需要删除的边数(不包括删除i和他父亲的连接的那条边(假设i不是根节点)

poj 2484 Cow Exhibition 【变形0-1背包】

题目:poj 2484 Cow Exhibition 题意:给出n头牛,每头牛有一个幸运值 si 和聪明值 ti ,现在要选出一些牛,让两个值的和最大,前提是sum(si)和sum(ti)都是非负值. 分析:此题数据量不大,可以暴搜+剪枝水过. 这里要说的是0-1背包的思想,这个题目明显的变形就是物品有两个属性值,而且都要选最大的. 那么我们可不可以把一个值固定下来来求另一个值的最大值,根据0-1背包的思想,定义状态:dp[i]表示装入一些物品使得sum(si)的时候最大的sum(ti)的值.

POJ 1699 Best Sequence (DFS+预处理)

题意:看那张图就一清二楚了吧, N个序列首位相连(相同的序列部分),得到最短的总序列. 题目链接:http://poj.org/problem?id=1699 ~~~~ 思路就是:将N个序列首尾相连能重合的长度求粗来.然后DFS枚举每种首尾相连的情况. #include<cstdio> #include<cstring> #include<algorithm> #define N 22 #define INF 0x7fffffff using namespace std

POJ 3132 Sum of Different Primes DP背包

http://poj.org/problem?id=3132 题意: 给定n和k,问用恰好k个不同的质数来表示n的方案数. 分析: n和k都很小.反正就是个背包,选k个物品恰好填满n即可. 1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 5 bool dp[1200][15]; 6 int ct[1200][15]; 7 int p[1200]; 8 bool a[1200]; 9 int n, k,

POJ 3411 Paid Roads(dfs)

*注:这一题很重要的是对与数据的处理和细节的把握把! http://poj.org/problem?id=3411 题目大意: 有n个城市,m条路,(0<=n,m<=10).从a到b,如果之前已经经过c点,那么付费p,否者付费r.求最小的费用,从1-->n! 注意: There may be more than one road connecting one city with another. so:你不能用map[a][b]存a->b的距离.只能有road [ i ]了. 还有

POJ 1321 棋盘问题 --- DFS

POJ 1321 棋盘问题 http://poj.org/problem?id=1321 /*POJ 1321 棋盘问题 --- DFS*/ #include <cstdio> #include <cstring> int n, k, cnt; bool visit[10]; //标记列的访问状态 char mapp[10][10]; /*从第r行开始正确放置p个棋子*/ void dfs(int r, int p){ if (p == 0){ ++cnt; return; } i