LightOJ 1071 Baker Vai(拆点+最大费用流)题解

题意:给一个n*m的方格,每个格子上都有一个数字表示价值,小A在左上角,他从左上角走到右下角只能向右或向下走,然后再从右下角走上左上角,这次只能向上或向左走,这两条路绝对没有重复,问你怎样走有最大价值。

思路:因为不能重复,就拆点。拆点是这样的,把一个点拆成一条边,每条边流量为1,表示只能走一次,费用为该点价值的相反数,因为要最大费用,初始和末尾两个点内的流量要设为2因为要走两次。我们把每个点和右边和下面的点相连,流量为1费用为0。最后我们得到最大的费用,但是要减去初始和末尾(算了两次)。

代码:

#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#define ll long long
const int maxn = 20000+5;
const int maxm = 100000+5;
const int MOD = 1e7;
const int INF = 1 << 25;
using namespace std;
struct Edge{
    int to,next,cap,flow,cost;
}edge[maxm];
int head[maxn],tot;
int pre[maxn],dis[maxn];
bool vis[maxn];
int N,M;
void init(){
    N = maxn;
    tot = 0;
    memset(head,-1,sizeof(head));
}
void addEdge(int u,int v,int cap,int cost){
    edge[tot].to = v;
    edge[tot].cap = cap;    //容量
    edge[tot].flow = 0;
    edge[tot].cost = cost;
    edge[tot].next = head[u];
    head[u] = tot++;

    edge[tot].to = u;
    edge[tot].cap = 0;
    edge[tot].flow = 0;
    edge[tot].cost = -cost;
    edge[tot].next = head[v];
    head[v] = tot++;
}
bool spfa(int s,int t){
    queue<int> q;
    for(int i = 0;i < N;i++){
        dis[i] = INF;
        vis[i] = false;
        pre[i] = -1;
    }
    dis[s] = 0;
    vis[s] = true;
    q.push(s);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        vis[u] = false;
        for(int i = head[u];i != -1;i = edge[i].next){
            int v = edge[i].to;
            if(edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost){
                dis[v] = dis[u] + edge[i].cost;
                pre[v] = i;
                if(!vis[v]){
                    vis[v] = true;
                    q.push(v);
                }
            }
        }
    }
    return pre[t] != -1;
}

int MCMF(int s,int t,int &cost){
    int flow = 0;
    cost = 0;
    while(spfa(s,t)){
        int MIN = INF;
        for(int i = pre[t];i != -1;i = pre[edge[i^1].to]){
            if(MIN > edge[i].cap - edge[i].flow){
                MIN = edge[i].cap - edge[i].flow;
            }
        }
        for(int i = pre[t];i != -1; i = pre[edge[i^1]. to]){
            edge[i]. flow += MIN;
            edge[i^1]. flow -= MIN;
            cost += edge[i]. cost * MIN;
        }
        flow += MIN;
    }
    return flow;
}

int n,m;
int mp[105][105];
int lpoint(int i,int j){ return m*(i - 1) + j; }
int rpoint(int i,int j){ return m*n + m*(i - 1) + j; }
int main(){
    int Case = 1,T;
    scanf("%d",&T);
    while(T--){
        init();
        scanf("%d%d",&n,&m);
        for(int i = 1;i <= n;i++){
            for(int j = 1;j <= m;j++){
                scanf("%d",&mp[i][j]);
                if((i == 1 && j == 1) || (i == n && j == m))   //拆点建边
                    addEdge(lpoint(i,j),rpoint(i,j),2,-mp[i][j]);
                else addEdge(lpoint(i,j),rpoint(i,j),1,-mp[i][j]);
            }
        }

        for(int i = 1;i <= n;i++){
            for(int j = 1;j <= m;j++){
                if(j != m)
                    addEdge(rpoint(i,j),lpoint(i,j + 1),1,0);
                if(i != n)
                    addEdge(rpoint(i,j),lpoint(i + 1,j),1,0);
            }
        }
        int cost;
        MCMF(lpoint(1,1),rpoint(n,m),cost);
        printf("Case %d: %d\n",Case++,-cost - mp[1][1] - mp[n][m]);
    }
    return 0;
}

 

posted @ 2018-07-24 18:38  KirinSB  阅读(218)  评论(0编辑  收藏  举报