【题目链接】
【POPOQQQ的题解】%一发建图姿势。
/* Pigonometry */ #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; const int maxn = 10005, maxm = 500005, maxq = 100000, inf = 0x3f3f3f3f; int n, m, head[maxn], cnt, depth[maxn], way[maxn], bg, ed, q[maxq], C[maxn], D[maxn], win[maxn], lose[maxn], du[maxn]; bool vis[maxn]; struct _edge { int v, w, c, next; } g[maxm << 1]; inline int iread() { int f = 1, x = 0; char ch = getchar(); for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1; for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0'; return f * x; } inline void add(int u, int v, int w, int c) { g[cnt] = (_edge){v, w, c, head[u]}; head[u] = cnt++; } inline void insert(int u, int v, int w, int c) { add(u, v, w, c); add(v, u, 0, -c); } inline bool spfa() { for(int i = 0; i <= ed; i++) depth[i] = inf; int h = 0, t = 0, u, i; depth[q[t++] = bg] = 0; while(h != t) for(i = head[u = q[h++]], vis[u] = 0; ~i; i = g[i].next) if(g[i].w && depth[g[i].v] > depth[u] + g[i].c) { depth[g[i].v] = depth[u] + g[i].c; way[g[i].v] = i; if(!vis[g[i].v]) vis[q[t++] = g[i].v] = 1; } return depth[ed] != inf; } inline int back() { int flow = inf, res = 0; for(int u = ed; u != bg; u = g[way[u] ^ 1].v) flow = min(flow, g[way[u]].w); for(int u = ed; u != bg; u = g[way[u] ^ 1].v) g[way[u]].w -= flow, g[way[u] ^ 1].w += flow, res += g[way[u]].c * flow; return res; } int main() { n = iread(); m = iread(); bg = 0; ed = n + m + 1; for(int i = 0; i <= ed; i++) head[i] = -1; cnt = 0; for(int i = 1; i <= n; i++) { win[i] = iread(); lose[i] = iread(); C[i] = iread(); D[i] = iread(); } for(int i = 1; i <= m; i++) { int u = iread(), v = iread(); insert(u, n + i, 1, 0); insert(v, n + i, 1, 0); insert(n + i, ed, 1, 0); du[u]++; du[v]++; } LL ans = 0; for(int i = 1; i <= n; i++) { ans += (LL)C[i] * win[i] * win[i] + (LL)D[i] * (du[i] + lose[i]) * (du[i] + lose[i]); for(int j = 1; j <= du[i]; j++) insert(bg, i, 1, C[i] * (win[i] * 2 + j * 2 - 1) - D[i] * ((du[i] + lose[i]) * 2 - j * 2 + 1)); } while(spfa()) ans += back(); printf("%lld\n", ans); return 0; }