费用流模版

浪的上下界费用流做法
把有下界的边(u,v,lowBound,upBound)
拆成两条
一条是(u,v)容量为lowBound,费用为-inf
然后另一条容量为upBound-lowBound ,费用正常
费用为原费用。。答案预先加上lowBound*费用
然后这个图跑一遍
答案对inf取个模


zkw费用流:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;
#define N 300005
#define M 3000055
#define inf 0x3f3f3f3f
struct MaxFlow
{
	int size, n;
	int st, en, maxflow, mincost;
	bool vis[N];
	int net[N], pre[N], cur[N], dis[N];
	queue <int> Q;
	struct EDGE	{
		int v, cap, cost, next;
		EDGE(){}
		EDGE(int a, int b, int c, int d)
		{	
			v = a, cap = b, cost = c, next = d;
		}
	}E[M<<1];
	void init(int _n)//传入点的个数[1-_n]
	{
		n = _n, size = 0;
		memset(net, -1, sizeof(net));
	}
	void add(int u, int v, int cap, int cost)
	{
		E[size] = EDGE(v, cap, cost, net[u]);
		net[u] = size++;
		E[size] = EDGE(u, 0, -cost, net[v]);
		net[v] = size++;
	}
	bool adjust()
	{
		int v, min = inf;
		for(int i = 0; i <= n; i++)
		{
			if(!vis[i])
				continue;
			for(int j = net[i]; v = E[j].v, j != -1; j = E[j].next)
				if(E[j].cap)
					if(!vis[v] && dis[v]-dis[i]+E[j].cost < min)
						min = dis[v] - dis[i] + E[j].cost;
		}
		if(min == inf)
			return false;
		for(int i = 0; i <= n; i++)
			if(vis[i])
				cur[i] = net[i], vis[i] = false, dis[i] += min;
		return true;
	}
	int augment(int i, int flow)
	{
		if(i == en)
		{
			mincost += dis[st] * flow;
			maxflow += flow;
			return flow;
		}
		vis[i] = true;
		for(int j = cur[i], v; v = E[j].v, j != -1; j = E[j].next)
		{
			if(!E[j].cap)
				continue;
			if(vis[v] || dis[v]+E[j].cost != dis[i])
				continue;
			int delta = augment(v, std::min(flow, E[j].cap));
			if(delta)
			{
				E[j].cap -= delta;
				E[j^1].cap += delta;
				cur[i] = j;
				return delta;
			}
		}
		return 0;
	}
	void spfa()
	{
		int u, v;
		for(int i = 0; i <= n; i++)
			vis[i] = false, dis[i] = inf;
		dis[st] = 0;
		Q.push(st);
		vis[st] = true;
		while(!Q.empty())
		{
			u = Q.front(), Q.pop();
			vis[u] = false;
			for(int i = net[u]; v = E[i].v, i != -1; i = E[i].next)
			{
				if(!E[i].cap || dis[v] <= dis[u] + E[i].cost)
					continue;
				dis[v] = dis[u] + E[i].cost;
				if(!vis[v])
				{
					vis[v] = true;
					Q.push(v);
				}
			}
		}
		for(int i = 0; i <= n; i++)
			dis[i] = dis[en] - dis[i];
	}
	int zkw(int s, int t)
	{
		st = s, en = t;
		spfa();
		mincost = maxflow = 0;
		for(int i = 0; i <= n; i++)
			vis[i] = false, cur[i] = net[i];
		do
		{
			while(augment(st, inf))
				memset(vis, false, sizeof(vis));
		}while(adjust());
		return mincost;
	}
}zkw;





白书费用流模版:
 
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<math.h>
using namespace std;
#define ll int
#define inf 0x3f3f3f3f
#define Inf 0x3FFFFFFFFFFFFFFFLL  
#define N 605*605*2
#define M 605*605*4
struct Edge {
    ll to, cap, cost, nex;
    Edge(){}
    Edge(ll to,ll cap,ll cost,ll next):to(to),cap(cap),cost(cost),nex(next){}
} edge[M<<1];
ll head[N], edgenum;
ll D[N], A[N], P[N];
bool inq[N];
void add(ll from,ll to,ll cap,ll cost) {
    edge[edgenum] = Edge(to,cap,cost,head[from]);
    head[from] = edgenum++;
    edge[edgenum] = Edge(from,0,-cost,head[to]);
    head[to] = edgenum++;
}
bool spfa(ll s, ll t, ll &flow, ll &cost) {
    for(ll i = 0; i <= t; i++) D[i] = inf;
    memset(inq, 0, sizeof inq);
    queue<ll>q;
    q.push(s);
    D[s] = 0; A[s] = inf;
    while(!q.empty()) {
        ll u = q.front(); q.pop();
        inq[u] = 0;
        for(ll i = head[u]; ~i; i = edge[i].nex)
        {
            Edge &e = edge[i];
            if(e.cap && D[e.to] > D[u] + e.cost)
            {
                D[e.to] = D[u] + e.cost;
                P[e.to] = i;
                A[e.to] = min(A[u], e.cap);
                if(!inq[e.to])
                {inq[e.to]=1; q.push(e.to);}
            }
        }
    }
	//若费用为inf则中止费用流
    if(D[t] == inf) return false;
    cost += D[t] * A[t];
    flow += A[t];
    ll u = t;
    while(u != s) {
        edge[ P[u] ].cap -= A[t];
        edge[P[u]^1].cap += A[t];
        u = edge[P[u]^1].to;
    }
    return true;
}
ll Mincost(ll s,ll t){
    ll flow = 0, cost = 0;
    while(spfa(s, t, flow, cost));
    return cost;
}
void init(){memset(head,-1,sizeof head); edgenum = 0;}




 
 
bzoj 2245,by neko13
 
预流推进模版

 http://hzaskywalker.blog.163.com/blog/static/20379815720123291371918/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值