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. 在Linux下锁住键盘和鼠标而不锁屏

    假如在你正看着屏幕上的某些重要的事情时,你不想让你的小猫或者小狗在你的键盘上行走,或者让你的孩子在键盘上瞎搞一气,那我建议你试试 xtrlock 这个工具. 假如在你正看着屏幕上的某些重要的事情时,你 ...

  2. JPA(四):EntityManager

    Persistence Persistence类使用于获取EntityManagerFactory实例,该类包含一个名为createEntityManagerFactory的静态方法. // 创建En ...

  3. Linux系统中关于Sqlite3中文乱码问题及解决办法

    新做的一个项目在本地(Win8)测试时没有问题,但传到服务器(Linux)时从Sqlite3数据库查询到的数据中文却是乱码(数据库中是正常的) 将php文件.html文件都设置成统一的utf8还是一样 ...

  4. Mongoose vs mongodb native driver – what to prefer?

      Paul Shan 7th Jun 2015 Mongoose or mongodb native driver, which one to use? This is one of the ini ...

  5. registry-1.docker.io TimeOut 错误

    用Docker For Windows在Windows 10上执行docker login或者 docker pull/push的时候,经常会报这样的错误: https: Get https://re ...

  6. MAC高效软件必备-落雨

    更新时间:2017年09月19日23:45:29 使用MAC有一年多,最想说的莫过于如何打造一个高效的使用Mac的体验. 1. MAC任务栏管理,窗口切换 1. Mac任务栏管理(类似于Windows ...

  7. 阿里云centos安装ftp与svn过程

    1.下载xshell或者secureCRT 2.登录centos或者服务器 3.安装vsftpd [root@xxx]# yum install vsftpd //安装vsftpd [root@xxx ...

  8. ExtMail telnet 25端口号 不通

    搭建好的Mail服务器在本地端口号25是开的,但是在别的电脑上就连不上. 修改/etc/postfix/main.cf文件,将 inet_interfaces = localhost 注释掉即可.

  9. Dijkstra和Floyd_warshall

    import java.util.Arrays; import java.util.Scanner; /*题目描写叙述: 有n个城市.城市间有m条道路.每条道路都有长度d.给你起点城市s终点终点t.要 ...

  10. Android缓存处理

    Android缓存: 採用缓存,能够进一步大大缓解数据交互的压力,又能提供一定的离线浏览.下边我简略列举一下缓存管理的适用环境: 1. 提供网络服务的应用 2. 数据更新不须要实时更新.哪怕是3-5分 ...