二次联通门 : BZOJ 1877: [SDOI2009]晨跑
/* BZOJ 1877: [SDOI2009]晨跑 拆点 + 费用流 */ #include <cstdio> #include <iostream> #define rg register inline void read (int &n) { rg char c = getchar (); for (n = 0; !isdigit (c); c = getchar ()); for (; isdigit (c); n = n * 10 + c - ‘0‘, c = getchar ()); } int S, T; #define Max 7000 #define INF 1e9 namespace net { const int MaxE = 2000000; int _n[MaxE], _v[MaxE], list[Max], _f[MaxE], _c[MaxE], EC = 1, d[Max], q[MaxE], pre[Max]; bool is[Max]; int c[Max]; inline void In (int u, int v, int f, int c) { _v[++ EC] = v, _n[EC] = list[u], list[u] = EC, _f[EC] = f, _c[EC] = c; _v[++ EC] = u, _n[EC] = list[v], list[v] = EC, _f[EC] = 0, _c[EC] = -c; } bool Bfs () { int h = 1, t = 1; q[t] = S; rg int i, n; for (i = 0; i <= T; ++ i) d[i] = INF, is[i] = false; for (d[S] = 0, c[S] = INF, pre[S] = 0; h <= t; ++ h) for (n = q[h], is[n] = false, i = list[n]; i; i = _n[i]) if (d[_v[i]] > d[n] + _c[i] && _f[i]) { d[_v[i]] = d[n] + _c[i], pre[_v[i]] = i, c[_v[i]] = std :: min (c[n], _f[i]); if (!is[_v[i]]) q[++ t] = _v[i], is[_v[i]] = true; } return d[T] < INF; } int Do () { int res = 0, p = 0; rg int i; for (int x; Bfs (); ++ p) { for (x = c[T], i = T; i != S; i = _v[pre[i] ^ 1]) _f[pre[i]] -= x, _f[pre[i] ^ 1] += x; res += d[T] * x; } printf ("%d %d", p, res); } } int main (int argc, char *argv[]) { int N, M; read (N), read (M); rg int i; S = 1, T = N << 1; int x, y, z; net :: In (S, S + N, INF, 0), net :: In (N, T, INF, 0); for (i = 1; i <= M; ++ i) read (x), read (y), read (z), net :: In (N + x, y, 1, z); for (i = 2; i < N; ++ i) net :: In (i, i + N, 1, 0); net :: Do (); return 0; }