【模板】最小费用最大流
仅仅是个模板
\(Code\)
#include<cstdio>
#include<queue>
#include<iostream>
#include<cstring>
using namespace std;
const int M = 50000 , N = 5000;
int n , m , s , t , dis[N + 5] , h[N + 5] , vis[N + 5] , pre[N + 5] , edge[N + 5] , flow[N + 5] , tot = 1 , Maxflow , Mincost;
queue<int> d;
struct node{
int to , nxt , w , f;
}e[M << 1 + 10];
void add(int u , int v , int w , int f)
{
e[++tot].to = v;
e[tot].w = w;
e[tot].f = f;
e[tot].nxt = h[u];
h[u] = tot;
}
int spfa()
{
memset(vis , 0 , sizeof(vis));
memset(dis , 127 , sizeof(dis));
memset(flow , 127 , sizeof(flow));
d.push(s);
vis[s] = 1 , dis[s] = 0 , pre[t] = -1;
while (!d.empty())
{
int now = d.front();
d.pop();
vis[now] = 0;
for(register int i = h[now]; i; i = e[i].nxt)
{
if (dis[e[i].to] > dis[now] + e[i].f && e[i].w)
{
dis[e[i].to] = dis[now] + e[i].f;
flow[e[i].to] = min(flow[now] , e[i].w);
pre[e[i].to] = now;
edge[e[i].to] = i;
if (!vis[e[i].to]) vis[e[i].to] = 1 , d.push(e[i].to);
}
}
}
return (pre[t] != -1);
}
int MCMF()
{
while (spfa())
{
Maxflow += flow[t];
Mincost += dis[t] * flow[t];
int now = t;
while (now != s)
{
e[edge[now]].w -= flow[t];
e[edge[now] ^ 1].w += flow[t];
now = pre[now];
}
}
}
int main()
{
scanf("%d%d%d%d" , &n , &m , &s , &t);
int u , v , w , f;
for(register int i = 1; i <= m; i++) scanf("%d%d%d%d" , &u , &v , &w , &f) , add(u , v , w , f) , add(v , u , 0 , -f);
MCMF();
printf("%d %d" , Maxflow , Mincost);
}