hdu 3657 Game【最大权独立点集------最大流最小割Dinic】

Game

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

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的矩阵,有k个必须拿的格子,对于n*m个格子,每个格子对应的价值告诉你,而且如果有两个相邻格子同时拿取,需要去掉两个格子的与的二倍的值。问最大拿取价值和。


思路:


1、首先这是一个二分图的模型,将n*m的矩阵分成两部分,对于(i+j)%2==0的设成一个集合,其他的设定成另一个集合,集合中的点没有相关。


2、将格子进行点编号:

e.g:2*2的一个格子:
1 2

3 4

3、黑白染色确定每个集合中的点之后,对于题干,如果我们这样要求:拿取了当前格子之后,其相邻的格子的值就不能拿了。(也先不考虑K个点的问题),如果是这样的一个题的模型,我们就可以这样建图:



其中14两点属于一个集合,23两点属于一个集合,源点到1和4的权值设定为其格子中的点权值。23两点到T的权值也设定为其格子中的点权值,1到2,、1到3、4到2、4到3的四条边权值设定为INF.那么这时候我们跑最大流,根据最小割最大流定理,我们求得的最大流的值,其实就等于最小抛弃值,我们设定sum==1234四个格子中的权值和,那么sum-maxflow得到的值,其实就是最大拿取值,我们最大流求得的值,就是最小抛弃值(最小割)。

那么总结建图规则:

①设定源点S,从源点连入(i+j)%2==0的那个集合中的所有点,设定其权值为其每个节点的点权。

②设定汇点T,将(i+j)%2==1的那个集合中的所有点连入汇点T中,设定其权值为其每个节点的点权。

③将每个属于(i+j)%2==0的那个集合的点,连入与其直接相邻的格子的那些个点,设定其原值为INF。

4、那么加上了相邻两个格子同时拿取需要去掉两个格子点权的与的二倍价值这个限制,我们又要如何建图呢?

其实不难考虑,我们直接将上述的第三条建边条件的权值设定为两个权值的与的二倍就可以了。其原因也不难想到:

对于S--->1--->2---->s这条增广路上,增加的流必然是瓶颈流,其实也就是点1的权,点2的权和需要去除的值三选1剔除。对于如果点1的权值最小,那么最终我们就相当于取点2的权。如果点2的权值最小,那么最终我们就相当于取点1的权,如果需要去除的值(两个格子都选择),那么最终我们就相当于取点1的权取点2的权然后去掉这个需要去掉的值。

所以最小割,割的就是最小抛弃的值,留下最大拿取的值。


5、那么如果再加上最后一条限制,有K个点必须拿取,那么我们就只需要在上述过程中,使得点1的权一定不会是最小的值即可,那么最终建图:

①设定源点S,从源点连入(i+j)%2==0的那个集合中的所有点,设定其权值为其每个节点的点权。(如果有某个点是必须拿的点,那么其权值设定为INF)

②设定汇点T,将(i+j)%2==1的那个集合中的所有点连入汇点T中,设定其权值为其每个节点的点权。(如果有某个点是必须拿的点,那么其权值设定为INF)

③将每个属于(i+j)%2==0的那个集合的点,连入与其直接相邻的格子的那些个点,设定其原值为INF。


6、思路最终确立,剩下的就是实现代码的过程了。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
using namespace std;
#define INF 0x3f3f3f3f
struct node
{
    int from;
    int to;
    int next;
    int w;
}e[100*100*50];
int head[100*100];
int cur[100*100];
int divv[100*100];
int a[100][100];
int vis[100][100];
int fx[4]={0,0,1,-1};
int fy[4]={1,-1,0,0};
int n,m,q,ss,tt,cont;
void add(int from,int to,int w)
{
    e[cont].from=from;
    e[cont].to=to;
    e[cont].w=w;
    e[cont].next=head[from];
    head[from]=cont++;
}
void getmap()
{
    cont=0;
    memset(head,-1,sizeof(head));
    ss=n*m+1;
    tt=ss+1;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if((i+j)%2==0)
            {
                if(vis[i][j]==0)
                {
                    add(ss,(i-1)*m+j,a[i][j]);
                    add((i-1)*m+j,ss,0);
                }
                else
                {
                    add(ss,(i-1)*m+j,INF);
                    add((i-1)*m+j,ss,0);
                }
            }
            else
            {
                if(vis[i][j]==0)
                {
                    add((i-1)*m+j,tt,a[i][j]);
                    add(tt,(i-1)*m+j,0);
                }
                else
                {
                    add((i-1)*m+j,tt,INF);
                    add(tt,(i-1)*m+j,0);
                }
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if((i+j)%2==0)
            {
                for(int k=0;k<4;k++)
                {
                    int x=i+fx[k];
                    int y=j+fy[k];
                    if(x>=1&&x<=n&&y>=1&&y<=m)
                    {
                        add((i-1)*m+j,(x-1)*m+y,2*(a[i][j]&a[x][y]));
                        add((x-1)*m+y,(i-1)*m+j,0);
                    }
                }
            }
        }
    }
}
int makedivv()
{
    memset(divv,0,sizeof(divv));
    queue<int >s;
    s.push(ss);
    divv[ss]=1;
    while(!s.empty())
    {
        int u=s.front();
        s.pop();
        if(u==tt)return  1;
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].to;
            int w=e[i].w;
            if(w&&divv[v]==0)
            {
                divv[v]=divv[u]+1;
                s.push(v);
            }
        }
    }
    return 0;
}
int Dfs(int u,int maxflow,int tt)
{
    if(u==tt)return maxflow;
    int ret=0;
    for(int &i=cur[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        int w=e[i].w;
        if(w&&divv[v]==divv[u]+1)
        {
            int f=Dfs(v,min(maxflow-ret,w),tt);
            e[i].w-=f;
            e[i^1].w+=f;
            ret+=f;
            if(ret==maxflow)return ret;
        }
    }
    return ret;
}
int Dinic()
{
    int ans=0;
    while(makedivv()==1)
    {
        memcpy(cur,head,sizeof(head));
        ans+=Dfs(ss,INF,tt);
    }
    return ans;
}
int main()
{
    while(~scanf("%d%d%d",&n,&m,&q))
    {
        memset(a,0,sizeof(a));
        memset(vis,0,sizeof(vis));
        int sum=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&a[i][j]);
                sum+=a[i][j];
            }
        }
        while(q--)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            vis[x][y]=1;
        }
        getmap();
        int ans=Dinic();
        //printf("%d %d\n",sum,ans);
        printf("%d\n",sum-ans);
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值