Special topics eleven kuangbin network flow Minimum Cost POJ - 2516

Topic links: https://vjudge.net/problem/POJ-2516

Ideas: For each good run minimum cost maximum flow, if all goods and people to build together chart run, O (v ^ 2 * m) of magnitude too large, time out.

Commodity stocks are split point between the goods store, in and out, play a limiting role.

Source -> people of the demand for commodities -> Stock enter -> Inventory to point out -> Meeting Point

The demand side of human flow between the source and people, people on the flow of goods between the INF side.

Source and human sides of fees, expenses 0 other side.

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;

const int N = 60,INF = (int)1e9;
int n,m,k,tot;
int head[N<<2],pre[N<<2],d[N<<2],vis[N<<2];
int p[N][N],g[N][N],c[N][N][N];
struct node{
    int to,nxt,cap,flow,cost;
}e[N*N];

inline void add(int u,int v,int cap,int flow,int cost){
    e[tot].to = v; e[tot].cap = cap; e[tot].flow = flow;
    e[tot].cost = cost; e[tot].nxt = head[u]; head[u] = tot++;

    e[tot].to = u; e[tot].cap = 0; e[tot].flow = flow;
    e[tot].cost = -cost; e[tot].nxt = head[v]; head[v] = tot++;
}

void input(int& sum_need){
    sum_need = 0;
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= k; ++j){
            scanf("%d",&p[i][j]);
            sum_need += p[i][j];
        }
    }
    for(int i = 1; i <= m; ++i){
        for(int j = 1; j <= k; ++j)
            scanf("%d",&g[i][j]);
    }
    for(int o = . 1 ; O <= K; ++ O) {
         for ( int I = . 1 ; I <= n-; ++ I) {
             for ( int J = . 1 ; J <= m; ++ J) 
                Scanf ( " % D " , & C [O] [I] [J]); 
        } 
    } 
} 

void build_map ( int O, int S, int T) {
     // 0 1 ~ n human source n + 1 ~ n + m Stock enter ,  
     // n-m +. 1 ~ + n-m + 2 * stock a point n + 2 * m + 1 sink. 
    for ( int I = . 1 ; I <= n-; ++i){
        for(int j = 1; j <= m; ++j){
            add(i,j+n,INF,0,c[o][i][j]);
        }
    }
    for(int i = 1; i <= n; ++i){
        add(s,i,p[i][o],0,0);
    }
    for(int i = 1; i <= m; ++i){
        add(i+n,i+n+m,g[i][o],0,0);
        add(i+n+m,t,INF,0,0);
    }
}

bool spfa(int s,int t){
    for(int i = s; i <= t; ++i){
        d[i] = INF; pre[i] = -1; vis[i] = false;
    }
    queue<int > que;
    que.push(s); d[s] = 0; vis[s] = true;
    while(!que.empty()){
        int now = que.front(); que.pop();
        vis[now] = false;
        for(int o = head[now]; ~o; o = e[o].nxt){
            int to = e[o].to;
            if(e[o].cap > e[o].flow && d[to] > d[now] + e[o].cost){
                d[to] = d[now] + e[o].cost;
                pre[to] = o;
                if(!vis[to]){
                    vis[to] = true;
                    que.push(to);
                }
            }
        }
    }
    if(pre[t] == -1) return false;
    else return true;
}

int mcmf(int s,int t,int& mf){

    int Min = INF,mc = 0;
    while(spfa(s,t)){
        Min = INF;
        for(int o = pre[t]; ~o; o = pre[e[o^1].to]){
            Min = min(Min,e[o].cap - e[o].flow);
        }
        for(int o = pre[t]; ~o; o = pre[e[o^1].to]){
            e[o].flow += Min;
            e[o^1].flow -= Min;
        }
        mf += Min;
        mc += d[t]*Min;
    }
    return mc;
}

void init_main(){
    memset(p,0,sizeof(p));
    memset(g,0,sizeof(g));
    memset(c,0,sizeof(c));
}

int main(){

    int s,t,mc,mf,sum_need;//商品总需求量
    while(~scanf("%d%d%d",&n,&m,&k) && (n+m+k)){
        init_main();
        input(sum_need);
        s = 0; t = n + 2*m + 1;
        mc = mf = 0;
        for(int o = 1; o <= k; ++o){//第o种商品
            for(int i = s; i <= t; ++i) head[i] = -1; tot = 0;
            build_map(o,s,t);
            mc += mcmf(s,t,mf);
        }
        //if(mf == sum_need) printf("-------------%d\n",mc);
        //else printf("--------------no\n");
        if(mf == sum_need) printf("%d\n",mc);
        else printf("-1\n");
    }

    return 0;
}

 

Guess you like

Origin www.cnblogs.com/SSummerZzz/p/12258875.html