最小费用最大流

#include <bits/stdc++.h>
using namespace std;


inline long long read() {
	char ch = getchar();
	long long f = 1,x = 0;
	while (ch > '9' || ch < '0') { if (ch == '-')f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); }
	return x * f;
}
//void ReadFile() {
//	FILE* stream1;
//	freopen_s(&stream1,"in.txt", "r", stdin);
//	freopen_s(&stream1,"out.txt", "w", stdout);
//}

static auto speedup = []() {ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); return nullptr; }();

#define HEAD(input) cf.head[input]
#define NEXT(input) cf.tail[input].nx
#define VALUE(input) cf.tail[input].v
#define TO(input) cf.tail[input].to
#define COST(input) cf.tail[input].cost
#define ADD(a,b,c,d) cf.add(a,b,c,d)

class ChainForward {
	struct ChainForwardNode {
		ChainForwardNode() :to(0), nx(0), v(0), cost(0){}
		int to, nx;
		long long v,cost;
	};
public:
	ChainForward(int headLen, int edgeLen) {
		init(headLen, edgeLen);
	}
	void init(int headLen, int edgeLen) {
		head.assign(headLen, 0);
		tail.assign(edgeLen, {});
		tot = 1;
	}
	int add(int from, int to, long long val,long long cost) {
		tail[++tot].to = to;
		tail[tot].v = val;
		tail[tot].cost = cost;
		tail[tot].nx = head[from];
		head[from] = tot;
		return tot;
	}
	int tot;
	vector<int> head;
	vector<ChainForwardNode> tail;
};
//点数 和 边数 最大值
ChainForward cf(10000, 2e5 + 7);

const int maxn = 5e3 + 7;
int  cost[maxn], flow[maxn],pre[maxn], n, m, s, t,maxflow = 0,maxCost = 0;
bool vis[maxn];

bool spfa() {
	memset(vis, 0, sizeof(vis));
	memset(cost, 0x7f, sizeof(cost));
	memset(flow, 0x7f, sizeof(flow));
	queue<int> q;
	q.push(s);
	vis[s] = 1;
	cost[s] = 0;
	pre[t] = -1;

	while (!q.empty()) {
		int now = q.front(); q.pop();
		vis[now] = 0;
		for (int i = HEAD(now); i; i = NEXT(i)) {
			int to = TO(i);
			if (VALUE(i) > 0 && cost[to] > cost[now] + COST(i)) {
				cost[to] = cost[now] + COST(i);
				pre[to] = i;
				flow[to] = min((long long)flow[now], VALUE(i));
				if (!vis[to]) {
					q.push(to);
					vis[to] = 1;
				}
			}
		}
	}
	return pre[t] != -1;
}
void MCMF() {
	while (spfa()) {
		int now = t;
		maxflow += flow[t];
		maxCost += cost[t] * flow[t];
		while (now != s) {
			int v = pre[now];
			VALUE(v) -= flow[t];
			VALUE(v ^ 1) += flow[t];
			now = TO(v ^ 1);
		}
	}
}
void slove() {
	cin >> n >> m >> s >> t;
	int a, b, c, d;
	for (int i = 1; i <= m; i++) {
		cin >> a >> b >> c >> d;
		ADD(a, b, c, d);
		ADD(b, a, 0, -d);
	}
	MCMF();
	cout << maxflow << " " << maxCost << endl;
}
int main() {
	slove();
	return 0;
}
class NetFlow2 {
public:
	typedef long long T;
	struct Edge {
		int to, id;
		T flow;
		T cost;
		Edge(int _to,  int _id,T _flow,T _cost) :to(_to),id(_id), flow(_flow),cost(_cost) {}
	};
	NetFlow2(int _n,int _s,int _t) {
		e.assign(_n + 1, vector<Edge>());
	}
	void Add(int from,int to, T flow,T cost) {
		int alen = e[from].size();
		int blen = e[to].size();
		e[from].push_back(Edge(to,blen,flow,cost));
		e[to].push_back(Edge(from,alen,0,-cost));
	}
	T &Value(Edge& edge) {
		return edge.flow;
	}
	int To(Edge& edge) {
		return edge.to;
	}
	int Pre(Edge& edge) {
		return edge.id;
	}
	T &Cost(Edge &edge){
		return edge.cost;
	}
	vector<vector<Edge> > e;
};

class MCMF:public NetFlow2{
public:
	static const T INF = 1e15;

	MCMF(int _n,int _s,int _t):NetFlow2(_n,_s,_t){
		n = _n;s = _s;t = _t;
		vis.assign(n + 1,false);
		cost.assign(n + 1,0);
		flow.assign(n + 1,0);
		last.assign(n + 1,0);
		pre.assign(n + 1,0);
	}

	bool spfa() {
		fill(vis.begin(),vis.end(),false);
		fill(cost.begin(),cost.end(),INF);
		fill(flow.begin(),flow.end(),INF);

		queue<int> q;
		q.push(s);
		vis[s] = 1;
		cost[s] = 0;
		pre[t] = -1;

		while (!q.empty()) {
			int now = q.front(); q.pop();
			vis[now] = 0;
			for(int i = 0;i < e[now].size();i++){
				Edge &x = e[now][i];
				int to = To(x);
				if(Value(x) > 0 && cost[to] > cost[now] + Cost(x)){
					cost[to] = cost[now] + Cost(x);
					pre[to] = i;
					last[to] = now;
					flow[to] = min(flow[now],Value(x));
					if(!vis[to]){
						q.push(to);
						vis[to] = 1;
					}
				}
			}
		}
		return pre[t] != -1;
	}

	pair<T,T> getAns(){
		T maxflow = 0,minCost = 0;

		while (spfa()) {
			int now = t;
			maxflow += flow[t];
			minCost += cost[t] * flow[t];
			while (now != s) {
				int a = last[now];
				int b = pre[now];
				Edge& edge = e[a][b];
				//去掉增广路
				Value(edge) -= flow[t];
				//增加反向边
				Edge& revEdge = e[now][Pre(edge)];
				Value(revEdge) += flow[t];
				//回退
				now = last[now];
			}
		}
		return make_pair(maxflow,minCost);
	}
private:
	int n,s,t;
	vector<bool > vis;
	vector<T> cost;
	vector<T> flow;
	vector<int> pre,last;
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值