网络流最小费用流模板

#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring> 
#define REP(i, a, b) for(int i = (a); i < (b); i++)
using namespace std;

typedef long long ll;
const int MAXN = 4123;
struct Edge
{
	int from, to, cap, flow, cost;
	Edge(int from, int to, int cap, int flow, int cost) : from(from), to(to), cap(cap), flow(flow), cost(cost) {};
};
vector<Edge> edges;
vector<int> g[MAXN];
int p[MAXN], a[MAXN], d[MAXN], vis[MAXN], n, m, s, t;

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

bool spfa(int& flow, ll& cost) //开longlong防炸 
{
	REP(i, 0, t + 1) d[i] = (i == s ? 0 : 1e9);
	memset(vis, 0, sizeof(vis));
	a[s] = 1e9; vis[s] = 1; p[s] = 0; 
	
	queue<int> q;
	q.push(s);
	while(!q.empty())
	{
		int u = q.front(); q.pop();
		vis[u] = 0;
		REP(i, 0, g[u].size())
		{
			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(!vis[e.to]) { vis[e.to] = 1; q.push(e.to); }
			}
		}
	}
	
	if(d[t] == 1e9) return false;
	flow += a[t]; //加的是a数组 
	cost += d[t] * a[t];
	for(int u = t; u != s; u = edges[p[u]].from)
	{
		edges[p[u]].flow += a[t]; //注意区分a数组和d数组,a数组是流量,d数组是费用 
		edges[p[u] ^ 1].flow -= a[t];
	}
	return true;
}

int mincost(ll& cost)
{
	int flow = 0; cost = 0;
	while(spfa(flow, cost));
	return flow;
}

int main()
{
	//输入略 
	ll ans;
	mincost(ans);
	printf("%lld\n", ans);
	return 0;	
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值