最小费用最大流模板

来源:互联网 发布:webvr源码 编辑:程序博客网 时间:2024/06/05 11:31
#include <iostream>#include <cstdio>#include <queue>#include <vector>using namespace std;#define ll long longstruct edge{    int u , v , f , c , next;    edge(int U = 0 , int V  = 0 , int F = 0 , int C = 0 , int Next = 0){        u = U , v = V , f = F , c = C , next = Next;    }}e[maxn*maxn];int vis[maxn] , pre[maxn] , head[maxn] ,cnt , source  , destinate;ll Max_flow , Min_cost , cost[maxn];void add(int u , int v , int f , int c){    e[cnt] = edge(u , v , f , c , head[u]) , head[u] = cnt++;    e[cnt] = edge(v , u , 0 , -1*c , head[v]) , head[v] = cnt++;}void ini(){    for(int i = 0; i < maxn; i++){        for(int j = 0; j < maxn; j++){            e[i*maxn+j] = edge(0 , 0 , 0 , 0 , -1);        }        vis[i] = 0;        pre[i] = -1;        cost[i] = inf;        head[i] = -1;    }    cnt = 0;    Max_flow = 0;    Min_cost = 0;}bool bfs(){    for(int i = 0; i < maxn; i++){        vis[i] = 0;        pre[i] = -1;        cost[i] = inf;    }    queue<int> q;    q.push(source);    cost[source] = 0;    while(!q.empty()){        int from = q.front();        q.pop();        vis[from] = 0;        for(int next = head[from]; next > -1; next = e[next].next){            int to = e[next].v;            if(e[next].f > 0 && cost[from]+e[next].c < cost[to]){                pre[to] = next;                cost[to] = cost[from]+e[next].c;                if(vis[to] == 0){                    vis[to] = 1;                    q.push(to);                }            }        }    }    if(pre[destinate] == -1) return false;    return true;}ll Max_Flow(){    while(bfs()){        ll flow = inf;        for(int from = pre[destinate]; from > -1; from = pre[e[from].u]){            flow = min(flow , (ll)e[from].f);        }        Min_cost += flow*cost[destinate];        Max_flow += flow;        for(int from = pre[destinate]; from > -1; from = pre[e[from].u]){            e[from].f -= flow;            e[from^1].f += flow;        }    }    return Max_flow;}int main(){    return 0;}

0 0
原创粉丝点击