HDU 3820 Golden Eggs 方格取数加强版

点击打开链接

 

Golden Eggs

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 311    Accepted Submission(s): 178


Problem Description
There is a grid with N rows and M columns. In each cell you can choose to put a golden or silver egg in it, or just leave it empty. If you put an egg in the cell, you will get some points which depends on the color of the egg. But for every pair of adjacent eggs with the same color, you lose G points if there are golden and lose S points otherwise. Two eggs are adjacent if and only if there are in the two cells which share an edge. Try to make your points as high as possible.
 


 

Input
The first line contains an integer T indicating the number of test cases.
There are four integers N, M, G and S in the first line of each test case. Then 2*N lines follows, each line contains M integers. The j-th integer of the i-th line Aij indicates the points you will get if there is a golden egg in the cell(i,j). The j-th integer of the (i+N)-th line Bij indicates the points you will get if there is a silver egg in the cell(i,j).

Technical Specification
1. 1 <= T <= 20
2. 1 <= N,M <= 50
3. 1 <= G,S <= 10000
4. 1 <= Aij,Bij <= 10000
 


 

Output
For each test case, output the case number first and then output the highest points in a line.
 


 

Sample Input
  
  
2 2 2 100 100 1 1 5 1 1 4 1 1 1 4 85 95 100 100 10 10 10 10 100 100
 


 

Sample Output
  
  
Case 1: 9 Case 2: 225
 


 

Author
hanshuai
 


 

Source
 


 

Recommend
lcy

 

 

牛人的分析:

对原矩阵黑白染色
1 2 3
4 5 6
7 8 9
A={1,3,5,7,9}
B={2,4,6,8}
矩阵中每个点可以取两个值中的任意一个,或者都不取。
根据这一个条件,我们可以把一个点拆分成两部分,k,k'。
对A集合中的点,k为金蛋,k'为银蛋。
B集合中的点,k为银蛋,k'为金蛋。
k->k’连一条容量为inf的边,这样就可以保证k,k'只取其中一个,或者都不取。
从S到k,从k'到T 分别连一条容量为其价值的边。
对A中的金蛋k,向B中的金蛋k'连一条容量为G的边。
对B中的银蛋k,向A中的银蛋k'连一条容量为S的边。
总的价值减去最小割,就是要求的价值。

 

//SAP算法
#include<stdio.h>
#include<string.h>
#define inf 9999999
#define M 5500
#define MIN(a,b) a>b?b:a;
struct E
{
    int v,w,next;
    E() {}
    E(int v,int w,int next):v(v),w(w),next(next) {}
} edg[500000];
int dis[M],gap[M],list[M],nodes;
int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
int sourse,sink,nn,node;
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,p,s=0,e,ans=0,count,k1,k2,t,a;
    scanf("%d",&t);
    for(int cas=1;cas<=t;cas++)
    {
        scanf("%d%d%d%d",&n,&m,&k1,&k2);
        memset(list,-1,sizeof(list));
        nodes=0;
        sourse=0;
        ans=0;
        count=n*m;
        e=2*n*m+1;
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
            {
                scanf("%d",&a);
                ans+=a;
                int flag=(i-1)*m+j;
                if((i+j)%2)
                    addedge(0,flag,a);
                else
                    addedge(flag+count,e,a);
            }
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
            {
                scanf("%d",&a);
                ans+=a;
                int flag=(i-1)*m+j;
                if((i+j)%2)
                    addedge(flag+count,e,a);
                else
                    addedge(0,flag,a);
            }
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
            {
                int flag=(i-1)*m+j;
                addedge(flag,flag+count,inf);
                for(int d=0;d<4;d++)
                {
                    int xx=i+dir[d][0];
                    int yy=j+dir[d][1];
                    if(xx<1||xx>n||yy<1||yy>m)continue;
                    int flag2=(xx-1)*m+yy;
                    if((i+j)%2)
                        addedge(flag,flag2+count,k1);
                    else
                        addedge(flag,flag2+count,k2);
                }

            }

        int anss=sap(s,e);
        //printf("%d %d\n",ans,anss);
        printf("Case %d: %d\n",cas,ans-anss);
    }
    return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值