费用流板子
还是一道板子题。。先练练手
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
}
const int N = 5005;
const int M = 50005;
int n, m, s, t, head[N], cnt, inq[N], pre[N], incf[N], dist[N], ans, mf;
struct Edge { int v, next, f, cost; } edge[M<<1];
void addEdge(int a, int b, int f, int cost){
edge[cnt].v = b, edge[cnt].f = f, edge[cnt].cost = cost, edge[cnt].next = head[a], head[a] = cnt ++;
edge[cnt].v = a, edge[cnt].f = 0, edge[cnt].cost = -cost, edge[cnt].next = head[b], head[b] = cnt ++;
}
bool spfa(){
full(dist, INF), full(inq, false);
full(pre, 0), full(incf, INF);
queue<int> q;
q.push(s);
dist[s] = 0, inq[s] = true;
while(!q.empty()){
int cur = q.front(); q.pop();
inq[cur] = false;
for(int i = head[cur]; i != -1; i = edge[i].next){
int u = edge[i].v;
if(edge[i].f > 0 && dist[u] > dist[cur] + edge[i].cost){
dist[u] = dist[cur] + edge[i].cost;
pre[u] = i, incf[u] = min(incf[cur], edge[i].f);
if(!inq[u]) q.push(u), inq[u] = true;
}
}
}
return dist[t] != INF;
}
void mcmf(){
while(spfa()){
int cur = t;
while(cur != s){
int i = pre[cur];
edge[i].f -= incf[t], edge[i^1].f += incf[t];
cur = edge[i^1].v;
}
mf += incf[t];
ans += dist[t] * incf[t];
}
}
int main(){
full(head, -1), cnt = 2;
n = read(), m = read(), s = read(), t = read();
for(int i = 0; i < m; i ++){
int u = read(), v = read(), w = read(), c = read();
addEdge(u, v, w, c);
}
mcmf();
printf("%d %d
", mf, ans);
return 0;
}