Game

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

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的矩阵,让你从中取出一定数量的数字。如果在矩阵中两两相邻的数字被取到的话需要付出一定的代价。而且给出某些点,规定这些点一定需要取到。求最多可以取到多少点。
 
大致思路:
    怎么说呢,这道题乍看上去和hdoj 1569:方格取数很相似,也很像是一个二分图的最大点权独立集问题。但是问题出的很巧妙,也就没有办法往模版上面套了。把矩阵中的点按照横纵坐标之和的奇偶性分成两个集合,设超级源汇点,源点第一个集合中的所有点连边,容量为这个点代表的数字的值。第二个集合中的所有点向汇点连边,容量也是这个点的值。第一个集合中点都向他周围的点连边,容量为他们同时被取时的消耗。如果一个点必须取,那就将他和源/汇点的容量设为inf,保证这条边不被割掉。用所有点的权值之和sum减去这个图的最小割得到的就是答案。总的来说,ac后的感受就是,这是一道需要意识流的题目
 
SAP():
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; const int VM=;
const int EM=;
const int INF=0x3f3f3f3f; struct Edge{
int to,nxt;
int cap;
}edge[EM<<]; int n,m,k,cnt,head[VM],map[][];
int dep[VM],gap[VM],cur[VM],aug[VM],pre[VM]; void addedge(int cu,int cv,int cw){
edge[cnt].to=cv; edge[cnt].cap=cw; edge[cnt].nxt=head[cu];
head[cu]=cnt++;
edge[cnt].to=cu; edge[cnt].cap=; edge[cnt].nxt=head[cv];
head[cv]=cnt++;
} int src,des; int SAP(int n){
int max_flow=,u=src,v;
int id,mindep;
aug[src]=INF;
pre[src]=-;
memset(dep,,sizeof(dep));
memset(gap,,sizeof(gap));
gap[]=n;
for(int i=;i<=n;i++)
cur[i]=head[i]; // 初始化当前弧为第一条弧
while(dep[src]<n){
int flag=;
if(u==des){
max_flow+=aug[des];
for(v=pre[des];v!=-;v=pre[v]){ // 路径回溯更新残留网络
id=cur[v];
edge[id].cap-=aug[des];
edge[id^].cap+=aug[des];
aug[v]-=aug[des]; // 修改可增广量,以后会用到
if(edge[id].cap==) // 不回退到源点,仅回退到容量为0的弧的弧尾
u=v;
}
}
for(int i=cur[u];i!=-;i=edge[i].nxt){
v=edge[i].to; // 从当前弧开始查找允许弧
if(edge[i].cap> && dep[u]==dep[v]+){ // 找到允许弧
flag=;
pre[v]=u;
cur[u]=i;
aug[v]=min(aug[u],edge[i].cap);
u=v;
break;
}
}
if(!flag){
if(--gap[dep[u]]==) /* gap优化,层次树出现断层则结束算法 */
break;
mindep=n;
cur[u]=head[u];
for(int i=head[u];i!=-;i=edge[i].nxt){
v=edge[i].to;
if(edge[i].cap> && dep[v]<mindep){
mindep=dep[v];
cur[u]=i; // 修改标号的同时修改当前弧
}
}
dep[u]=mindep+;
gap[dep[u]]++;
if(u!=src) // 回溯继续寻找允许弧
u=pre[u];
}
}
return max_flow;
} int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d%d%d",&n,&m,&k)){
cnt=;
memset(head,-,sizeof(head)); src=; des=n*m+;
int sum=;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
scanf("%d",&map[i][j]);
sum+=map[i][j];
}
int x,y;
while(k--){
scanf("%d%d",&x,&y);
if((x+y)%==)
addedge(src,(x-)*m+y,INF);
else
addedge((x-)*m+y,des,INF);
}
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
int tmp=(i-)*m+j;
if((i+j)%==)
addedge(src,tmp,map[i][j]);
else
addedge(tmp,des,map[i][j]);
}
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if((i+j)%==){
int tmp=(i-)*m+j;
if(i>) addedge(tmp,tmp-m,*(map[i][j]&map[i-][j]));
if(i<n) addedge(tmp,tmp+m,*(map[i][j]&map[i+][j]));
if(j>) addedge(tmp,tmp-,*(map[i][j]&map[i][j-]));
if(j<m) addedge(tmp,tmp+,*(map[i][j]&map[i][j+]));
}
printf("%d\n",sum-SAP(des+));
}
return ;
}

上面用SAP算法,只用了78ms,而下面的Dinic用了1600ms,Orz。。。。。。。。。

Dinic():

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; const int VM=;
const int EM=;
const int INF=0x3f3f3f3f; struct Edge{
int to,nxt;
int cap;
}edge[EM<<]; int n,m,k,cnt,head[VM],src,des;
int map[][],dep[VM]; //dep[i]表示当前点到起点src的层数 void addedge(int cu,int cv,int cw){
edge[cnt].to=cv; edge[cnt].cap=cw; edge[cnt].nxt=head[cu];
head[cu]=cnt++;
edge[cnt].to=cu; edge[cnt].cap=; edge[cnt].nxt=head[cv];
head[cv]=cnt++;
} int BFS(){ // 重新建图(按层数建图)
queue<int> q;
while(!q.empty())
q.pop();
memset(dep,-,sizeof(dep));
dep[src]=;
q.push(src);
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=head[u];i!=-;i=edge[i].nxt){
int v=edge[i].to;
if(edge[i].cap> && dep[v]==-){ // 如果可以到达且还没有访问
dep[v]=dep[u]+;
q.push(v);
}
}
}
return dep[des]!=-;
} int DFS(int u,int minx){ // 查找路径上的最小的流量
if(u==des)
return minx;
int tmp;
for(int i=head[u];i!=-;i=edge[i].nxt){
int v=edge[i].to;
if(edge[i].cap> && dep[v]==dep[u]+ && (tmp=DFS(v,min(minx,edge[i].cap)))){
edge[i].cap-=tmp; //正向减少
edge[i^].cap+=tmp; //反向增加
return tmp;
}
}
return ;
} int Dinic(){
int ans=,tmp;
while(BFS()){
while(){
tmp=DFS(src,INF);
if(tmp==)
break;
ans+=tmp;
}
}
return ans;
}
int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d%d%d",&n,&m,&k)){
cnt=;
memset(head,-,sizeof(head)); src=; des=n*m+;
int sum=;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
scanf("%d",&map[i][j]);
sum+=map[i][j];
}
int x,y;
while(k--){
scanf("%d%d",&x,&y);
if((x+y)%==)
addedge(src,(x-)*m+y,INF);
else
addedge((x-)*m+y,des,INF);
}
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
int tmp=(i-)*m+j;
if((i+j)%==)
addedge(src,tmp,map[i][j]);
else
addedge(tmp,des,map[i][j]);
}
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if((i+j)%==){
int tmp=(i-)*m+j;
if(i>) addedge(tmp,tmp-m,*(map[i][j]&map[i-][j]));
if(i<n) addedge(tmp,tmp+m,*(map[i][j]&map[i+][j]));
if(j>) addedge(tmp,tmp-,*(map[i][j]&map[i][j-]));
if(j<m) addedge(tmp,tmp+,*(map[i][j]&map[i][j+]));
}
printf("%d\n",sum-Dinic());
}
return ;
}

下面的EK算法直接超时了,暂且不知道是不是还有什么优化,。。。。。。。

EK():

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; const int VM=;
const int EM=;
const int INF=0x3f3f3f3f; struct Edge{
int to,nxt;
int cap;
}edge[EM<<]; int n,m,k,cnt,head[VM],max_flow; //max_flow是最大流
int map[][],flow[][]; // map[i][j]是每条边的容量,flow[i][j]是每条边的流量
int res[VM],pre[VM]; //res[]是每个点的剩余流量,pre[]是每个点的父亲 void addedge(int cu,int cv,int cw){
edge[cnt].to=cv; edge[cnt].cap=cw; edge[cnt].nxt=head[cu];
head[cu]=cnt++;
edge[cnt].to=cu; edge[cnt].cap=; edge[cnt].nxt=head[cv];
head[cv]=cnt++;
} int EK(int src,int des){
max_flow=;
queue<int> q;
while(!q.empty())
q.pop();
memset(flow,,sizeof(flow)); //最开始每条边的流量都是0
while(){
memset(res,,sizeof(res)); //残余流量得变0,一开始所有点都没流入对吧
res[src]=INF; //源点嘛,剩余流量无限是必须的...
q.push(src); //从源点开始进行BFS找增广路
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=head[u];i!=-;i=edge[i].nxt){ //遍历所有点,找可行边
int v=edge[i].to;
if(!res[v] && edge[i].cap>flow[u][v]){ //该点剩余流量为0 且 容量大于流量,也就是找到了新的结点
pre[v]=u; //找到新结点,父节点得记录一下吧
q.push(v);
res[v]=min(res[u],edge[i].cap-flow[u][v]); //如果u的剩余流量能填满uv就填满,不能的话就把u这点的流量全部流向uv
}
}
}
if(res[des]==) //如果当前已经是最大流,汇点没有残余流量
return max_flow;
for(int u=des;u!=src;u=pre[u]){ //如果还能增广,那么回溯,从汇点往回更新每条走过的边的流量
flow[pre[u]][u]+=res[des]; //更新正向流量 (注意这里更新的是流量,而不是容量)
flow[u][pre[u]]-=res[des]; //更新反向流量
}
max_flow+=res[des];
}
} int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d%d%d",&n,&m,&k)){
cnt=;
memset(head,-,sizeof(head)); int src=, des=n*m+;
int sum=;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
scanf("%d",&map[i][j]);
sum+=map[i][j];
}
int x,y;
while(k--){
scanf("%d%d",&x,&y);
if((x+y)%==)
addedge(src,(x-)*m+y,INF);
else
addedge((x-)*m+y,des,INF);
}
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
int tmp=(i-)*m+j;
if((i+j)%==)
addedge(src,tmp,map[i][j]);
else
addedge(tmp,des,map[i][j]);
}
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if((i+j)%==){
int tmp=(i-)*m+j;
if(i>) addedge(tmp,tmp-m,*(map[i][j]&map[i-][j]));
if(i<n) addedge(tmp,tmp+m,*(map[i][j]&map[i+][j]));
if(j>) addedge(tmp,tmp-,*(map[i][j]&map[i][j-]));
if(j<m) addedge(tmp,tmp+,*(map[i][j]&map[i][j+]));
}
printf("%d\n",sum-EK(src,des));
}
return ;
}

HDU 3657 Game (SAP | Dinic | EK 三种算法的比较)的更多相关文章

  1. 最短路问题的三种算法&模板

    最短路算法&模板 最短路问题是图论的基础问题.本篇随笔就图论中最短路问题进行剖析,讲解常用的三种最短路算法:Floyd算法.Dijkstra算法及SPFA算法,并给出三种算法的模板.流畅阅读本 ...

  2. c语言求回文数的三种算法的描述

    c语言求回文数的三种算法的描述 题目描述 注意:(这些回文数都没有前导0) 1位的回文数有0,1,2,3,4,5,6,7,8,9 共10个: 2位的回文数有11,22,33,44,55,66,77,8 ...

  3. Java利用DES/3DES/AES这三种算法分别实现对称加密

    转载地址:http://blog.csdn.net/smartbetter/article/details/54017759 有两句话是这么说的: 1)算法和数据结构就是编程的一个重要部分,你若失掉了 ...

  4. 内存分配---FF、BF、WF三种算法

    动态分区分配是根据进程的实际需要,动态的为之分配内存空间.而在实现可变分区分配时,将涉及到分区分配中 所用的数据结构.分区分配算法和分区的分配与内存回收的过程. 分区分配中的数据结构:(1)描述空闲块 ...

  5. 图文实例解析,InnoDB 存储引擎中行锁的三种算法

    前文提到,对于 InnoDB 来说,随时都可以加锁(关于加锁的 SQL 语句这里就不说了,忘记的小伙伴可以翻一下上篇文章),但是并非随时都可以解锁.具体来说,InnoDB 采用的是两阶段锁定协议(tw ...

  6. HDOJ--1869--六度分离(用三种算法写的,希望能比較出来他们之间的差别)

    六度分离 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  7. 常见算法:C语言求最小公倍数和最大公约数三种算法

    最小公倍数:数论中的一种概念,两个整数公有的倍数成为他们的公倍数,当中一个最小的公倍数是他们的最小公倍数,相同地,若干个整数公有的倍数中最小的正整数称为它们的最小公倍数,维基百科:定义点击打开链接 求 ...

  8. C语言求最小公倍数和最大公约数三种算法(经典)

    把以前写的一些经验总结汇个总,方便给未来的学弟学妹们做个参考! --------------------------永远爱你们的:Sakura 最小公倍数:数论中的一种概念,两个整数公有的倍数成为他们 ...

  9. 缓存算法(FIFO 、LRU、LFU三种算法的区别)

    FIFO算法 FIFO 算法是一种比较容易实现的算法.它的思想是先进先出(FIFO,队列),这是最简单.最公平的一种思想,即如果一个数据是最先进入的,那么可以认为在将来它被访问的可能性很小.空间满的时 ...

随机推荐

  1. GIT 如何在不提交Commit的情况下切换分支

    最近遇到一个问题,事情是这样子的,刚刚接到客户说他的项目有问题,于是就打开本地的源码查看经过排查确定了问题,于是就开始进行修正工作 将问题修复好准备提交到git的时候发现当前的分支是不对的,但问题是我 ...

  2. 在javascript中substr和substring的区别是什么

      1.substring 用于提取字符串中介于两个指定下标之间的字符 substring(start,end) 开始和结束的位置,从零开始的索引 substring 方法返回的子串包括 start ...

  3. 持续集成之代码质量管理-Sonar [三]

    转载:https://www.abcdocker.com/abcdocker/2053 摘要 Sonar 是一个用于代码质量管理的开放平台.通过插件机制,Sonar 可以集成不同的测试工具,代码分析工 ...

  4. js escape 与php escape

    javascript有编码函数escape()和对应的解码函数unescape(),而php中只有个urlencode和urldecode,这个编码和解码函数对encodeURI和encodeURIC ...

  5. springmvc转换JSON数据

    1.引入jackson包 要想在springmvc框架下支持json的转换,需要引入jackson的包,在pom.xml中添加如下代码: <dependency> <groupId& ...

  6. ZH奶酪:PHP 使用DOMDocument操作XML

    原文链接:http://my.oschina.net/zhangb081511/blog/160113 PHP写XML方法很多,这里主要介绍一下DOMDocument的用法,跟 JS大体上相同,其实非 ...

  7. bash if 表达式含义

    [ -a FILE ] 如果 FILE 存在则为真. [ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真. [ -c FILE ] 如果 FILE 存在且是一个字特殊文件则为真. [ ...

  8. 神器phpstorm功能具体解释

    phpstorm包括了webstorm的所有功能,更可以支持php代码. PhpStorm是一个轻量级且便捷的PHP IDE,其旨在提供用户效率,可深刻理解用户的编码,提供智能代码补全,高速导航以及即 ...

  9. SSH使用Slf4j

    1. Slf4j的使用 在上一篇随笔:SSH使用Log4j的基础上配置. (1)导入两个文件:slf4j-api-1.5.8.jar和slf4j-log4j12-1.5.8.jar. (2)在需要日志 ...

  10. Audition CC2019 MME设备内部错误怎么解决!

    安装完了AA2019,没有想到像昨天安装系统那么不顺利... 当然出现问题的原因是因为我安装了win10 并且我禁用了麦克风的所有应用权限. 设置里面搜索 麦克风 权限 Ok工作啦, 好开森~     ...