poj3422 拆点法x->x'建立两条边+最小费用最大流

/**
题目:poj3422 拆点法+最小费用最大流
链接:http://poj.org/problem?id=3422
题意:给定n*n的矩阵,含有元素值,初始sum=0.每次从最左上角开始出发,每次向右或者向下一格。终点是右下角。
每经过一个格子,获取它的值,并把该格子的值变成0.问经过k次从左上角到右下角。能得到的数值和最大多少。

思路:我觉得本题元素值全是非负数。要不然不可以过。很多网上的博客代码在有负数情况下过不了。
拆点法+最小费用最大流

建图:
每一个格子x,拆成x,xi, x向xi连两条边,其一:x->xi,cap=1,cost=-wx;其二:x->xi,cap=k-1,cost=0;表示x这个格子可以经过k次,
第一次获得值为wx,之后经过它只能获得0.

左上角格子x,   s->x,cap=k,cost=0;
右下角格子x,   xi->t,cap=k,coste=0;

如果x的右边的格子或者下面的格子是y,  xi->y,cap=k,cost=0;



*/
#include<iostream>
#include<cstring>
#include<vector>
#include<map>
#include<cstdio>
#include<sstream>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int N = 5500;
struct Edge{
    int from, to, cap, flow, cost;
    Edge(int u,int v,int c,int f,int w):from(u),to(v),cap(c),flow(f),cost(w){}
};
struct MCMF{
    int n, m;
    vector<Edge> edges;
    vector<int> G[N];
    int inq[N];
    int d[N];
    int p[N];
    int a[N];

    void init(int n){
        this->n = n;
        for(int i = 0; i <= n; i++) G[i].clear();
        edges.clear();
    }

    void AddEdge(int from,int to,int cap,long long cost){
        edges.push_back(Edge(from,to,cap,0,cost));
        edges.push_back(Edge(to,from,0,0,-cost));
        m = edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool BellmanFord(int s,int t,int &flow,long long &cost){
        for(int i = 0; i <= n; i++) d[i] = INF;
        memset(inq, 0, sizeof inq);
        d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF;

        queue<int>  Q;
        Q.push(s);
        while(!Q.empty()){
            int u = Q.front(); Q.pop();
            inq[u] = 0;
            for(int i = 0; i < G[u].size(); i++){
                Edge& e = edges[G[u][i]];
                if(e.cap>e.flow&&d[e.to]>d[u]+e.cost){
                    d[e.to] = d[u]+e.cost;
                    p[e.to] = G[u][i];
                    a[e.to] = min(a[u],e.cap-e.flow);
                    if(!inq[e.to]) {Q.push(e.to); inq[e.to] = 1;}
                }
            }
        }
        if(d[t]==INF) return false;
        flow += a[t];
        cost += (long long)d[t]*(long long)a[t];
        for(int u = t; u!=s; u = edges[p[u]].from){
            edges[p[u]].flow+=a[t];
            edges[p[u]^1].flow-=a[t];
        }
        return true;
    }
    int MincostMaxflow(int s,int t,long long &cost){
        int flow = 0;
        cost = 0;
        while(BellmanFord(s,t,flow,cost));
        return flow;
    }
};
int main()
{
    int n, k;
    while(scanf("%d%d",&n,&k)==2)
    {
        int w, s = 0, t = n*n+1;
        MCMF mcmf;
        mcmf.init(t*2);
        mcmf.AddEdge(s,1,k,0);
        for(int i = 1; i <= n; i++){
            for(int j = 1;j <= n; j++){
                scanf("%d",&w);
                int x = (i-1)*n+j, y = x+t;
                mcmf.AddEdge(x,y,1,-w);
                mcmf.AddEdge(x,y,k-1,0);
                if(i==n&&j==n){
                    mcmf.AddEdge(y,t,k,0);
                }
                if(j+1<=n){
                    int m = (i-1)*n+j+1;
                    mcmf.AddEdge(y,m,k,0);
                }
                if(i+1<=n){
                    int m = i*n+j;
                    mcmf.AddEdge(y,m,k,0);
                }
            }
        }
        LL cost;
        mcmf.MincostMaxflow(s,t,cost);
        printf("%lld\n",-cost);
    }
    return 0;
}

 

posted on 2017-07-23 14:24  hnust_accqx  阅读(495)  评论(0编辑  收藏  举报

导航