hdu 3657 Game(最小割,最大点权独立集)

46 篇文章 0 订阅

Game

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 799    Accepted Submission(s): 333


Problem Description
onmylove has invented a game on n × m grids. There is one positive integer on each grid. Now you can take the numbers from the grids to make your final score as high as possible. The way to get score is like
the following:
● At the beginning, the score is 0;
● If you take a number which equals to x, the score increase x;
● If there appears two neighboring empty grids after you taken the number, then the score should be decreased by 2(x&y). Here x and y are the values used to existed on these two grids. Please pay attention that "neighboring grids" means there exits and only exits one common border between these two grids.

Since onmylove thinks this problem is too easy, he adds one more rule:
● Before you start the game, you are given some positions and the numbers on these positions must be taken away.
Can you help onmylove to calculate: what's the highest score onmylove can get in the game?
 

Input
Multiple input cases. For each case, there are three integers n, m, k in a line.
n and m describing the size of the grids is n ×m. k means there are k positions of which you must take their numbers. Then following n lines, each contains m numbers, representing the numbers on the n×m grids.Then k lines follow. Each line contains two integers, representing the row and column of one position
and you must take the number on this position. Also, the rows and columns are counted start from 1.
Limits: 1 ≤ n, m ≤ 50, 0 ≤ k ≤ n × m, the integer in every gird is not more than 1000.
 

Output
For each test case, output the highest score on one line.
 

Sample Input
  
  
2 2 1 2 2 2 2 1 1 2 2 1 2 7 4 1 1 1
 

Sample Output
  
  
4 9
Hint
As to the second case in Sample Input, onmylove gan get the highest score when calulating like this: 2 + 7 + 4 - 2 × (2&4) - 2 × (2&7) = 13 - 2 × 0 - 2 × 2 = 9.
题意:有n*m的格子,每个格子上有一个数,初始分数为0,从格子取出一个数,则分数能加上这个数。规定如果取出的两个数x和y存在相邻的情况,则分数要减去2*(x&y),并必须取规定的k个数。问最后能得到的最大的分数。
思路:和方格取数类似,只不过加上了一些规定。
     
     
// 1. 先染色,取一个点染白色,和它相邻的点染黑色; // 2. 每个白点x向它相邻的黑点y连一条边,容量为 2*(x&y),表示若x和y都取,则付出的代价为2*(x&y); // 3. 增加源点S,向每一个白色点连一条边,若这个数必须取,则容量为INF(表示若不取这个数,则付出的代价为INF,所以最小割中一定不包含这条边),否则容量为白点的权; // 4. 增加汇点T,每个黑点向T连一条边,若这个数必须取,则容量为INF(理由同上),否则 容量为黑点的权


AC代码:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <ctime>
#include <algorithm>
#define ll __int64

using namespace std;

const int INF = 1000000000;
const int maxn = 2505;

int d[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
struct Edge
{
    int u, v, cap, flow, next;
} et[maxn * maxn];
int low[maxn], cnt[maxn], dis[maxn], cur[maxn], pre[maxn], eh[maxn];
int s, t, n, m, k, num;
int G[55][55];
bool must[55][55];
bool ok(int x, int y)
{
    if(x >= 1 && x <= n && y >= 1 && y <= m) return true;
    return false;
}
void init()
{
    memset(eh, -1, sizeof(eh));
    num = 0;
}
void add(int u, int v, int cap, int flow)
{
    Edge e = {u, v, cap, flow, eh[u]};
    et[num] = e;
    eh[u] = num++;
}
void addedge(int u, int v, int cap)
{
    add(u, v, cap, 0);
    add(v, u, 0, 0);
}
int isap(int s, int t, int nv)
{
    int u, v, now, flow = 0;
    memset(cnt, 0, sizeof(cnt));
    memset(dis, 0, sizeof(dis));
    memset(low, 0, sizeof(low));
    for(u = 0; u <= nv; u++) cur[u] = eh[u];
    low[s] = INF, cnt[0] = nv, u =s;
    while(dis[s] < nv)
    {
        for(now = cur[u]; now != -1; now = et[now].next)
            if(et[now].cap - et[now].flow && dis[u] == dis[v = et[now].v] + 1) break;
        if(now != -1)
        {
            cur[u] = pre[v] = now;
            low[v] = min(et[now].cap - et[now].flow, low[u]);
            u = v;
            if(u == t)
            {
                for(; u != s; u = et[pre[u]].u)
                {
                    et[pre[u]].flow += low[t];
                    et[pre[u]^1].flow -= low[t];
                }
                flow += low[t];
                low[s] = INF;
            }
        }
        else
        {
            if(--cnt[dis[u]] == 0) break;
            dis[u] = nv, cur[u] = eh[u];
            for(now = eh[u]; now != -1; now = et[now].next)
                if(et[now].cap - et[now].flow && dis[u] > dis[et[now].v] + 1)
                    dis[u] = dis[et[now].v] + 1;
            cnt[dis[u]]++;
            if(u != s) u = et[pre[u]].u;
        }
    }
    return flow;
}
int main()
{
    int a, b;
    while(~scanf("%d%d%d", &n, &m, &k))
    {
        init();
        memset(must, false, sizeof(must));
        s = 0;
        t = n * m + 1;
        int sum = 0;
        for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
        {
            scanf("%d", &G[i][j]);
            sum += G[i][j];
        }
        while(k--)
        {
            scanf("%d%d", &a, &b);
            must[a][b] = true;
        }

        for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
        {
            if((i + j)&1)
            {
                if(must[i][j]) addedge((i - 1) * m + j, t, INF);
                else addedge((i - 1) * m + j, t, G[i][j]);
            }
            else
            {
                if(must[i][j]) addedge(s, (i - 1) * m + j, INF);
                else addedge(s, (i - 1) * m + j, G[i][j]);

                for(int k = 0; k < 4; k++)
                {
                    int nx = i + d[k][0], ny = j + d[k][1];
                    if(ok(nx, ny))
                    addedge((i - 1) * m + j, (nx - 1) * m + ny, 2 * (G[i][j] & G[nx][ny]));
                }
            }
        }
        printf("%d\n", sum - isap(s, t, t + 1));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值