• HDU 3657 Game (SAP | Dinic | EK 三种算法的比较)


    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=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[i]=head[i]; // 初始化当前弧为第一条弧
        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[u];i!=-1;i=edge[i].nxt){
                v=edge[i].to;    // 从当前弧开始查找允许弧
                if(edge[i].cap>0 && dep[u]==dep[v]+1){  // 找到允许弧
                    flag=1;
                    pre[v]=u;
                    cur[u]=i;
                    aug[v]=min(aug[u],edge[i].cap);
                    u=v;
                    break;
                }
            }
            if(!flag){
                if(--gap[dep[u]]==0)    /* gap优化,层次树出现断层则结束算法 */
                    break;
                mindep=n;
                cur[u]=head[u];
                for(int i=head[u];i!=-1;i=edge[i].nxt){
                    v=edge[i].to;
                    if(edge[i].cap>0 && dep[v]<mindep){
                        mindep=dep[v];
                        cur[u]=i;   // 修改标号的同时修改当前弧
                    }
                }
                dep[u]=mindep+1;
                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=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[i][j]);
                    sum+=map[i][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[i][j]);
                    else
                        addedge(tmp,des,map[i][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[i][j]&map[i-1][j]));
                        if(i<n)    addedge(tmp,tmp+m,2*(map[i][j]&map[i+1][j]));
                        if(j>1)    addedge(tmp,tmp-1,2*(map[i][j]&map[i][j-1]));
                        if(j<m)    addedge(tmp,tmp+1,2*(map[i][j]&map[i][j+1]));
                    }
            printf("%d
    ",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[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=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[u];i!=-1;i=edge[i].nxt){
                int v=edge[i].to;
                if(edge[i].cap>0 && dep[v]==-1){      // 如果可以到达且还没有访问
                    dep[v]=dep[u]+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[u];i!=-1;i=edge[i].nxt){
            int v=edge[i].to;
            if(edge[i].cap>0 && dep[v]==dep[u]+1 && (tmp=DFS(v,min(minx,edge[i].cap)))){
                edge[i].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[i][j]);
                    sum+=map[i][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[i][j]);
                    else
                        addedge(tmp,des,map[i][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[i][j]&map[i-1][j]));
                        if(i<n)    addedge(tmp,tmp+m,2*(map[i][j]&map[i+1][j]));
                        if(j>1)    addedge(tmp,tmp-1,2*(map[i][j]&map[i][j-1]));
                        if(j<m)    addedge(tmp,tmp+1,2*(map[i][j]&map[i][j+1]));
                    }
            printf("%d
    ",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[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=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[u];i!=-1;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]==0)     //如果当前已经是最大流,汇点没有残余流量
                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=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[i][j]);
                    sum+=map[i][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[i][j]);
                    else
                        addedge(tmp,des,map[i][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[i][j]&map[i-1][j]));
                        if(i<n)    addedge(tmp,tmp+m,2*(map[i][j]&map[i+1][j]));
                        if(j>1)    addedge(tmp,tmp-1,2*(map[i][j]&map[i][j-1]));
                        if(j<m)    addedge(tmp,tmp+1,2*(map[i][j]&map[i][j+1]));
                    }
            printf("%d
    ",sum-EK(src,des));
        }
        return 0;
    }
  • 相关阅读:
    每日总结
    每日总结
    每日总结
    每日总结
    每日总结
    每日总结
    每日总结
    实现微信小程序和支付宝小程序二维码合并
    Solr教程:1.下载和安装
    如何生成漫画风图片无需下载APP无需PS无需电脑
  • 原文地址:https://www.cnblogs.com/jackge/p/3238836.html
Copyright © 2020-2023  润新知