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. WPF装饰元素

    Border: <Style TargetType="Border"> <Setter Property="CornerRadius" Val ...

  2. Android性能优化系列之App启动优化

    Android性能优化系列之布局优化 Android性能优化系列之内存优化 Android性能优化系列之apk瘦身 应用的启动速度缓慢是我们在开发过程中常常会遇到的问题,比方启动缓慢导致的黑屏.白屏问 ...

  3. GitHub没有实时通知怎么办?当然是自己上手写一个啊!

    相信各位程序员对github已经不陌生了.不知道各位有没有注意到GitHub没有推送通知这个功能.当有人在我的存储库中创建了一个提取请求/问题时,我可以收到电子邮件通知,但当有人stars/forks ...

  4. Android Context完全解析,你所不知道的Context的各种细节

    Context相信所有的Android开发人员基本上每天都在接触,因为它太常见了.但是这并不代表Context没有什么东西好讲的,实际上Context有太多小的细节并不被大家所关注,那么今天我们就来学 ...

  5. asp.net集合类

    1.返回IEnumerable类型 protected void Page_Load(object sender, EventArgs e) { IEnumerable ie = AllGet(); ...

  6. dcm4che,WADO相关

    关于 dcm4che WADO WADO:Web Access to DICOM Objects dcm4che 是一个为医疗保健企业的开源应用程序和工具集合.这些应用程序已经开发了Java编程语言的 ...

  7. 如何随机获取数据库不连续ID的数据?

    这个问题的来由是我朋友要为一网站实现一个标签云功能,和我交流后我给出了一个方案,在此略作记录,亦求拍砖. 大概需求这是样的: 在数据库有一张表A如下图: 其中id字段的值未必是连续的,现在我朋友要做的 ...

  8. 使用Spring框架入门四:基于注解的方式的AOP的使用

    一.简述 前面讲了基于XML配置的方式实现AOP,本文简单讲讲基于注解的方式实现. 基于注解的方式实现前,要先在xml配置中通过配置aop:aspectj-autoproxy来启用注解方式注入. &l ...

  9. 树莓派中找不到/dev/video0的解决方案及RaspberryCam的使用

    一.原因 当使用CSI连接的方式将摄像头模块连接树莓派后,在/dev/中找不到video0,因此使用一些第三方库(如Opencv或RaspberryCam)去调用摄像头时,无法调用成功. 二.解决方法 ...

  10. CentOS静默安装Oracle 11gR2(x64)

    环境 OS: CentOS 7.4; hosts: L134; IP: 192.168.1.134 DB: linux.x64_11gR2_database 安装依赖包 yum install -y ...