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

本文介绍了一种在特定游戏网格中选取数值以达到最高得分的算法。通过建立超级源点和超级汇点,并利用最小割等于最大流的原则,解决了包含必选格子的复杂网格取数问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击打开链接

 

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;
}


 

 

 

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,击“新建项”,选择“Pipeline”。 输入项目名称和描述,击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值