#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; }
网络流最小费用流模板
猜你喜欢
转载自blog.csdn.net/qq_34416123/article/details/80469092
今日推荐
周排行