最小费用最大流模板

来源:互联网 发布:该域名访问升级 编辑:程序博客网 时间:2024/05/22 00:35
typedef long long ll;const int maxn = 1000005;const int INF = 0x3f3f3f3f;const int e_maxn = 200000 * 4;const int v_maxn = 15000;struct ppp{int v,nex,cap,flow,c;}e[e_maxn];int head[v_maxn],pre[v_maxn],inq[v_maxn],a[v_maxn],dis[v_maxn];int tole,N,M,s,t;void make_edge(int u,int v,int cap,int c){e[tole].v = v;e[tole].cap = cap;e[tole].c = c;e[tole].flow = 0;e[tole].nex = head[u]; head[u] = tole++;}void add_edge(int u,int v,int cap,int c){make_edge(u,v,cap,c);make_edge(v,u,0,-c);}queue<int> que;void maxflow_mincost(int s,int t,int &flow,int &cost){int temp,v,u;while(1){mem(inq,0);mem(dis,0x3f);a[s] = INF;dis[s] = 0;inq[s] = 1;que.push(s);mem(pre,-1);pre[s] = 0;while(!que.empty()){temp = que.front();que.pop();inq[temp] = 0;for(int i = head[temp];~i;i = e[i].nex){v = e[i].v;if(e[i].cap > e[i].flow && dis[v] > dis[temp] + e[i].c){dis[v] = dis[temp] + e[i].c;pre[v] = i;a[v] = min(a[temp],e[i].cap - e[i].flow);if(!inq[v]){que.push(v);inq[v] = 1;}}}}if(pre[t] == -1)break;flow += a[t];cost += dis[t] * a[t];for(u = t;u != s;u = e[pre[u] ^ 1].v){e[pre[u]].flow += a[t];e[pre[u] ^ 1].flow -= a[t];}}}

0 0
原创粉丝点击