HDU 3657 Game 最大点独立集(最小割)

点击打开链接

 

Game

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


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.
 


 

Author
onmylove
 


 

Source
 


 

Recommend
lcy
 

 

给你一个n*m的矩阵,每个格子里面都有一个整数,让你从中随便选出几个数使其和最大,如果两个格子相邻,则减去2*(x&y),x和y分别是相邻的两个格子里面的数,其中还有一些格子是必须要选的。

这道题比较像方格取数。建一个超级源点和超级汇点,超级源点连接x,y坐标之和为偶数的方格,超级汇点连接x,y坐标之和为奇数的方格。如果两个格子相连,则它们之间连一条边,权值为2*(x&y)。对于必须选的点,将它们与源点或者汇点相连的权值为无穷大,设为无穷大的话,程序就不会割这条边了,这个点就会留下来。

根据最小割==最大流,即可求出最小割。最后的结果就是所有格子的总和--最小割。

 

#include<stdio.h>
#include<string.h>
#define inf 0x3f3f3f3f
#define MIN(a,b) a>b?b:a;
#define M 2510
struct E
{
    int v,w,next;
    E() {}
    E(int v,int w,int next):v(v),w(w),next(next) {}
} edg[140007];
int dis[M],gap[M],list[M],nodes;
int sourse,sink,nn,node;
int g[57][57];
void addedge(int u,int v,int w)
{
    edg[nodes]=E(v,w,list[u]);
    list[u]=nodes++;
    edg[nodes]=E(u,0,list[v]);
    list[v]=nodes++;
}
int dfs(int src,int aug)
{
    if(src==sink)return aug;
    int left=aug,mindis=nn;
    for(int j=list[src]; j!=-1; j=edg[j].next)
    {
        int v=edg[j].v;
        if(edg[j].w)
        {
            if(dis[v]+1==dis[src])
            {
                int minn=MIN(left,edg[j].w);
                minn=dfs(v,minn);
                edg[j].w-=minn;
                edg[j^1].w+=minn;
                left-=minn;
                if(dis[sourse]>=nn)return aug-left;
                if(left==0)break;
            }
            if(dis[v]<mindis)
                mindis=dis[v];
        }
    }

    if(left==aug)
    {
        if(!(--gap[dis[src]]))dis[sourse]=nn;
        dis[src]=mindis+1;
        gap[dis[src]]++;
    }
    return aug-left;
}
int sap(int s,int e)
{
    int ans=0;
    nn=e+1;
    memset(dis,0,sizeof(dis));
    memset(gap,0,sizeof(gap));
    gap[0]=nn;
    sourse=s;
    sink=e;
    while(dis[sourse]<nn)
        ans+=dfs(sourse,inf);
    return ans;
}
int main()
{
    int n,m,k;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        int s,e,h,a,b,ans=0;
        memset(list,-1,sizeof(list));
        int p,sum=0,maxx=0;
        int ss=n*m+1;
        e=n*m+2;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&g[i][j]);
                ans+=g[i][j];
            }
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                int s=(i-1)*m+j;
                if((i+j)%2==0)
                {
                    addedge(ss,s,g[i][j]);
                    if(i>1)addedge(s,s-m,2*(g[i][j]&g[i-1][j]));
                    if(i<n)addedge(s,s+m,2*(g[i][j]&g[i+1][j]));
                    if(j>1)addedge(s,s-1,2*(g[i][j]&g[i][j-1]));
                    if(j<m)addedge(s,s+1,2*(g[i][j]&g[i][j+1]));
                }
                else
                {
                    addedge(s,e,g[i][j]);
                    /*if(i>1)addedge(s,s-m,2*(g[i][j]&g[i-1][j]));
                    if(i<n)addedge(s,s+m,2*(g[i][j]&g[i+1][j]));
                    if(j>1)addedge(s,s-1,2*(g[i][j]&g[i][j-1]));
                    if(j<m)addedge(s,s+1,2*(g[i][j]&g[i][j+1]));*/
                }

            }

        for(int i=1;i<=k;i++)
        {
            scanf("%d%d",&a,&b);
            if((a+b)%2==0)addedge(ss,(a-1)*m+b,inf);
                else addedge((a-1)*m+b,e,inf);
        }
        nodes=0;
        sourse=0;
        int anss=sap(ss,e);
        printf("%d\n",ans-anss);
    }
    return 0;
}


 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值