看了题解才会建图...
对于点x,从S向x连容量为va的边,从x向T连容量为vb的边。
对于边(u, v),从S向u和v分别连两条容量为ea / 2的边,从u和v向T分别连两条容量为eb / 2的边,然后u和v中间互相连两条容量为ea / 2 + eb / 2 + ec的边。
答案是 最大贡献 减去 最小割。
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 10005, maxm = 40005, maxq = 50000, inf = 0x3f3f3f3f;
int n, m, head[maxn], cur[maxn], cnt, bg, ed, depth[maxn], q[maxq];
struct _edge {
int v, w, next;
} g[4 * maxn + 10 * maxm];
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) {
g[cnt] = (_edge) {v, w, head[u]};
head[u] = cnt++;
}
bool bfs() {
for(int i = bg; i <= ed; i++) depth[i] = -1;
int h = 0, t = 0, u, i; depth[q[t++] = bg] = 1;
while(h != t) for(i = head[u = q[h++]]; ~i; i = g[i].next) if(g[i].w && !~depth[g[i].v]) {
depth[g[i].v] = depth[u] + 1;
if(g[i].v == ed) return 1;
q[t++] = g[i].v;
}
return 0;
}
int dfs(int x, int flow) {
if(x == ed) return flow;
int left = flow;
for(int i = cur[x]; ~i; i = g[i].next) if(g[i].w && depth[g[i].v] == depth[x] + 1) {
int tmp = dfs(g[i].v, min(left, g[i].w));
left -= tmp; g[i].w -= tmp; g[i ^ 1].w += tmp;
if(g[i].w) cur[x] = i;
if(!left) return flow;
}
if(left == flow) depth[x] = -1;
return flow - left;
}
int main() {
n = iread(); m = iread(); bg = 0; ed = n + 1;
for(int i = bg; i <= ed; i++) head[i] = -1;
LL ans = 0;
add(bg, 1, inf); add(1, bg, 0); add(n, ed, inf); add(ed, n, 0);
for(int i = 2; i < n; i++) {
int va = iread(); va <<= 1;
add(bg, i, va); add(i, bg, 0);
ans += va;
}
for(int i = 2; i < n; i++) {
int vb = iread(); vb <<= 1;
add(i, ed, vb); add(ed, i, 0);
ans += vb;
}
for(int i = 1; i <= m; i++) {
int x = iread(), y = iread(), ea = iread(), eb = iread(), ec = iread();
add(bg, x, ea); add(x, bg, 0);
add(bg, y, ea); add(y, bg, 0);
add(x, ed, eb); add(ed, x, 0);
add(y, ed, eb); add(ed, y, 0);
add(x, y, ea + eb + (ec << 1)); add(y, x, ea + eb + (ec << 1));
ans += ea + eb << 1;
}
while(bfs()) {
for(int i = bg; i <= ed; i++) cur[i] = head[i];
ans -= dfs(bg, inf);
}
printf("%lld\n", ans >> 1);
return 0;
}