跑得很慢
#include <bits/stdc++.h>
using namespace std;
#define N 5005
#define M 100005
#define inf 0x3f3f3f3f
typedef pair<int, int> pii;
int S, T;
struct MCMF
{
int hd[N], nxt[M], to[M], c[M], w[M], tot = 1, Cost, Flow;
void add(int u, int v, int C, int W)
{
nxt[++tot] = hd[u], to[tot] = v, c[tot] = C, w[tot] = W, hd[u] = tot;
nxt[++tot] = hd[v], to[tot] = u, c[tot] = 0, w[tot] = -W, hd[v] = tot;
}
void clear() { memset(hd, 0, sizeof(hd)), tot = 1; }
int dis[N], cur[N];
queue<int> q;
bitset<N> in, vis;
bool SPFA()
{
vis.reset();
memset(dis, 0x3f, sizeof(dis));
memcpy(cur, hd, sizeof(cur));
q.push(S);
dis[S] = 0, in[S] = 1;
while (!q.empty())
{
int u = q.front();
q.pop(), in[u] = 0;
for (int i = hd[u]; i; i = nxt[i])
{
int v = to[i];
if (!c[i]) continue;
if (dis[v] > dis[u] + w[i])
{
dis[v] = dis[u] + w[i];
if (!in[v])
{
q.push(v);
in[v] = 1;
}
}
}
}
return dis[T] != inf;
}
int DFS(int u, int now)
{
vis[u] = 1;
if (u == T) return now;
int res = 0;
for (int i = cur[u]; i && now; i = nxt[i])
{
int v = to[i], C = c[i];
cur[u] = i;
if (dis[v] != dis[u] + w[i] || !C || vis[v]) continue;
int flow = DFS(v, min(C, now));
Cost += flow * w[i];
c[i] -= flow, c[i ^ 1] += flow;
now -= flow, res += flow;
}
return res;
}
void SSP()
{
int x;
while (SPFA())
while ((x = DFS(S, inf))) Flow += x;
}
} G;
int n, m;
int main()
{
cin >> n >> m >> S >> T;
while (m--)
{
int u, v, c, w;
cin >> u >> v >> c >> w;
G.add(u, v, c, w);
}
G.SSP();
cout << G.Flow << ' ' << G.Cost;
return 0;
}