设为首页 收藏本站
查看: 701|回复: 0

[经验分享] HDU 3657 Game (SAP | Dinic | EK 三种算法的比较)

[复制链接]

尚未签到

发表于 2015-9-18 13:31:32 | 显示全部楼层 |阅读模式
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

2010 Asia Regional Chengdu Site —— Online Contest

  


Recommend

lcy

  大致题意:


    给出一个n*m的矩阵,让你从中取出一定数量的数字。如果在矩阵中两两相邻的数字被取到的话需要付出一定的代价。而且给出某些点,规定这些点一定需要取到。求最多可以取到多少点。



大致思路:

    怎么说呢,这道题乍看上去和hdoj 1569:方格取数很相似,也很像是一个二分图的最大点权独立集问题。但是问题出的很巧妙,也就没有办法往模版上面套了。把矩阵中的点按照横纵坐标之和的奇偶性分成两个集合,设超级源汇点,源点第一个集合中的所有点连边,容量为这个点代表的数字的值。第二个集合中的所有点向汇点连边,容量也是这个点的值。第一个集合中点都向他周围的点连边,容量为他们同时被取时的消耗。如果一个点必须取,那就将他和源/汇点的容量设为inf,保证这条边不被割掉。用所有点的权值之和sum减去这个图的最小割得到的就是答案。总的来说,ac后的感受就是,这是一道需要意识流的题目



SAP():




#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int VM=101000;
const int EM=500100;
const int INF=0x3f3f3f3f;
struct Edge{
int to,nxt;
int cap;
}edge[EM<<1];
int n,m,k,cnt,head[VM],map[110][110];
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=0;   edge[cnt].nxt=head[cv];
head[cv]=cnt++;
}
int src,des;
int SAP(int n){
int max_flow=0,u=src,v;
int id,mindep;
aug[src]=INF;
pre[src]=-1;
memset(dep,0,sizeof(dep));
memset(gap,0,sizeof(gap));
gap[0]=n;
for(int i=0;i<=n;i++)
cur=head; // 初始化当前弧为第一条弧
while(dep[src]<n){
int flag=0;
if(u==des){
max_flow+=aug[des];
for(v=pre[des];v!=-1;v=pre[v]){     // 路径回溯更新残留网络
id=cur[v];
edge[id].cap-=aug[des];
edge[id^1].cap+=aug[des];
aug[v]-=aug[des];   // 修改可增广量,以后会用到
if(edge[id].cap==0) // 不回退到源点,仅回退到容量为0的弧的弧尾
u=v;
}
}
for(int i=cur;i!=-1;i=edge.nxt){
v=edge.to;    // 从当前弧开始查找允许弧
if(edge.cap>0 && dep==dep[v]+1){  // 找到允许弧
flag=1;
pre[v]=u;
cur=i;
aug[v]=min(aug,edge.cap);
u=v;
break;
}
}
if(!flag){
if(--gap[dep]==0)    /* gap优化,层次树出现断层则结束算法 */
break;
mindep=n;
cur=head;
for(int i=head;i!=-1;i=edge.nxt){
v=edge.to;
if(edge.cap>0 && dep[v]<mindep){
mindep=dep[v];
cur=i;   // 修改标号的同时修改当前弧
                }
}
dep=mindep+1;
gap[dep]++;
if(u!=src)  // 回溯继续寻找允许弧
u=pre;
}
}
return max_flow;
}
int main(){
//freopen("input.txt","r",stdin);
while(~scanf("%d%d%d",&n,&m,&k)){
cnt=0;
memset(head,-1,sizeof(head));
src=0; des=n*m+1;
int sum=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
scanf("%d",&map[j]);
sum+=map[j];
}
int x,y;
while(k--){
scanf("%d%d",&x,&y);
if((x+y)%2==0)
addedge(src,(x-1)*m+y,INF);
else
addedge((x-1)*m+y,des,INF);
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
int tmp=(i-1)*m+j;
if((i+j)%2==0)
addedge(src,tmp,map[j]);
else
addedge(tmp,des,map[j]);
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if((i+j)%2==0){
int tmp=(i-1)*m+j;
if(i>1)    addedge(tmp,tmp-m,2*(map[j]&map[i-1][j]));
if(i<n)    addedge(tmp,tmp+m,2*(map[j]&map[i+1][j]));
if(j>1)    addedge(tmp,tmp-1,2*(map[j]&map[j-1]));
if(j<m)    addedge(tmp,tmp+1,2*(map[j]&map[j+1]));
}
printf("%d\n",sum-SAP(des+1));
}
return 0;
}
  
  上面用SAP算法,只用了78ms,而下面的Dinic用了1600ms,Orz。。。。。。。。。
  Dinic():



#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int VM=101000;
const int EM=500100;
const int INF=0x3f3f3f3f;
struct Edge{
int to,nxt;
int cap;
}edge[EM<<1];
int n,m,k,cnt,head[VM],src,des;
int map[110][110],dep[VM];    //dep表示当前点到起点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=0;   edge[cnt].nxt=head[cv];
head[cv]=cnt++;
}
int BFS(){      // 重新建图(按层数建图)
queue<int> q;
while(!q.empty())
q.pop();
memset(dep,-1,sizeof(dep));
dep[src]=0;
q.push(src);
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=head;i!=-1;i=edge.nxt){
int v=edge.to;
if(edge.cap>0 && dep[v]==-1){      // 如果可以到达且还没有访问
dep[v]=dep+1;
q.push(v);
}
}
}
return dep[des]!=-1;
}
int DFS(int u,int minx){    // 查找路径上的最小的流量
if(u==des)
return minx;
int tmp;
for(int i=head;i!=-1;i=edge.nxt){
int v=edge.to;
if(edge.cap>0 && dep[v]==dep+1 && (tmp=DFS(v,min(minx,edge.cap)))){
edge.cap-=tmp;     //正向减少
edge[i^1].cap+=tmp;     //反向增加
return tmp;
}
}
return 0;
}
int Dinic(){
int ans=0,tmp;
while(BFS()){
while(1){
tmp=DFS(src,INF);
if(tmp==0)
break;
ans+=tmp;
}
}
return ans;
}
int main(){
//freopen("input.txt","r",stdin);
while(~scanf("%d%d%d",&n,&m,&k)){
cnt=0;
memset(head,-1,sizeof(head));
src=0; des=n*m+1;
int sum=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
scanf("%d",&map[j]);
sum+=map[j];
}
int x,y;
while(k--){
scanf("%d%d",&x,&y);
if((x+y)%2==0)
addedge(src,(x-1)*m+y,INF);
else
addedge((x-1)*m+y,des,INF);
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
int tmp=(i-1)*m+j;
if((i+j)%2==0)
addedge(src,tmp,map[j]);
else
addedge(tmp,des,map[j]);
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if((i+j)%2==0){
int tmp=(i-1)*m+j;
if(i>1)    addedge(tmp,tmp-m,2*(map[j]&map[i-1][j]));
if(i<n)    addedge(tmp,tmp+m,2*(map[j]&map[i+1][j]));
if(j>1)    addedge(tmp,tmp-1,2*(map[j]&map[j-1]));
if(j<m)    addedge(tmp,tmp+1,2*(map[j]&map[j+1]));
}
printf("%d\n",sum-Dinic());
}
return 0;
}
  下面的EK算法直接超时了,暂且不知道是不是还有什么优化,。。。。。。。
  EK():



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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-115470-1-1.html 上篇帖子: BPC (9) 下篇帖子: SAP ABAP 点滴记录
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表