#define endl "\n" using ll = longlong; using db = double;
constint N = 1e6 + 10; constint INF = 0x3f3f3f3f;
structDicnic { staticconstint M = 1e6 + 10; staticconstint N = 2e2 + 10;
structEdge { int to, nxt, flow;
Edge() {}
Edge(int to, int nxt, int flow) : to(to), nxt(nxt), flow(flow) {} } edge[M << 1];
int S, T; int head[N], dep[N], tot;
voidInit(){ memset(head, -1, sizeof head); tot = 0; }
voidset(int _S, int _T){ S = _S, T = _T; }
voidaddedge(int u, int v, int w, int rw = 0){ edge[tot] = Edge(v, head[u], w); head[u] = tot++; edge[tot] = Edge(u, head[v], rw); head[v] = tot++; }
boolBFS(){ memset(dep, -1, sizeof dep); queue<int> q; q.push(S); dep[S] = 1; while (!q.empty()) { int u = q.front(); q.pop(); for (int i = head[u]; ~i; i = edge[i].nxt) { if (edge[i].flow && dep[edge[i].to] == -1) { dep[edge[i].to] = dep[u] + 1; q.push(edge[i].to); } } } return dep[T] >= 0; }
intDFS(int u, int f){ if (u == T || f == 0) return f; int w, used = 0; for (int i = head[u]; ~i; i = edge[i].nxt) { if (edge[i].flow && dep[edge[i].to] == dep[u] + 1) { w = DFS(edge[i].to, min(f - used, edge[i].flow)); edge[i].flow -= w; edge[i ^ 1].flow += w; used += w; if (used == f) return f; } } if (!used) dep[u] = -1; return used; }
intgao(){ int res = 0; while (BFS()) { res += DFS(S, INF); } return res; } } dicnic;
int n, m, low[N], totFlow[N];
voidRUN(){ memset(totFlow, 0, sizeof totFlow); scanf("%d %d", &n, &m); dicnic.Init(); int S = 0, T = n + 1; dicnic.set(S, T); for (int i = 1, u, v, w; i <= m; ++i) { scanf("%d %d %d %d", &u, &v, low + i, &w); dicnic.addedge(u, v, w - low[i]); totFlow[u] -= low[i]; totFlow[v] += low[i]; } int sum = 0; for (int i = 1; i <= n; ++i) { if (totFlow[i] >= 0) { sum += totFlow[i]; dicnic.addedge(S, i, totFlow[i]); } else { dicnic.addedge(i, T, -totFlow[i]); } } int tmp = dicnic.gao(); if (tmp == sum) { puts("YES"); for (int i = 1; i <= m; ++i) { printf("%d\n", low[i] + dicnic.edge[2 * i - 1].flow); } } else { puts("NO"); } puts(""); }
#define endl "\n" using ll = longlong; using db = double;
constint N = 1e3 + 10; constint M = 2e3 + 10; constint INF = 0x3f3f3f3f;
structDicnic { staticconstint M = 1e6 + 10; staticconstint N = 3e3 + 10;
structEdge { int to, nxt, flow;
Edge() {}
Edge(int to, int nxt, int flow) : to(to), nxt(nxt), flow(flow) {} } edge[M << 1];
int S, T; int head[N], dep[N], tot;
voidInit(){ memset(head, -1, sizeof head); tot = 0; }
voidset(int _S, int _T){ S = _S, T = _T; }
voidaddedge(int u, int v, int w, int rw = 0){ edge[tot] = Edge(v, head[u], w); head[u] = tot++; edge[tot] = Edge(u, head[v], rw); head[v] = tot++; }
boolBFS(){ memset(dep, -1, sizeof dep); queue<int> q; q.push(S); dep[S] = 1; while (!q.empty()) { int u = q.front(); q.pop(); for (int i = head[u]; ~i; i = edge[i].nxt) { if (edge[i].flow && dep[edge[i].to] == -1) { dep[edge[i].to] = dep[u] + 1; q.push(edge[i].to); } } } return dep[T] > 0; }
intDFS(int u, int f){ if (u == T || f == 0) return f; int w, used = 0; for (int i = head[u]; ~i; i = edge[i].nxt) { if (edge[i].flow && dep[edge[i].to] == dep[u] + 1) { w = DFS(edge[i].to, min(f - used, edge[i].flow)); edge[i].flow -= w; edge[i ^ 1].flow += w; used += w; if (used == f) return f; } } if (!used) dep[u] = -1; return used; }
intgao(){ int res = 0; while (BFS()) { res += DFS(S, INF); } return res; } } dicnic;
int n, m, low[N][M], totFlow[N * M], pos[N][M];
voidRUN(){ memset(pos, -1, sizeof pos); memset(totFlow, 0, sizeof totFlow); int S = 0, T = n + m + 1; dicnic.Init(); for (int i = 1, x; i <= m; ++i) { scanf("%d", &x); dicnic.addedge(i + n, T, INF - x); totFlow[i + n] -= x; totFlow[T] += x; } for (int i = 1, x, y; i <= n; ++i) { scanf("%d %d", &x, &y); dicnic.addedge(S, i, y); for (int j = 1, ith, up, down; j <= x; ++j) { scanf("%d %d %d", &ith, &down, &up); ++ith; pos[i][ith] = dicnic.tot; dicnic.addedge(i, n + ith, up - down); totFlow[i] -= down; totFlow[n + ith] += down; low[i][ith] = down; } } int sum = 0; dicnic.addedge(T, S, INF); int SS = n + m + 2, TT = n + m + 3; for (int i = S; i <= T; ++i) { if (totFlow[i] < 0) { dicnic.addedge(i, TT, -totFlow[i]); } if (totFlow[i] > 0) { sum += totFlow[i]; dicnic.addedge(SS, i, totFlow[i]); } } dicnic.set(SS, TT); if (dicnic.gao() == sum) { dicnic.set(S, T); int res = dicnic.gao(); printf("%d\n", res); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (pos[i][j] != -1) { printf("%d\n", low[i][j] + dicnic.edge[pos[i][j] ^ 1].flow); } } } } else { puts("-1"); } puts(""); }
structDicnic { staticconstint M = 1e6 + 10; staticconstint N = 3e3 + 10;
structEdge { int to, nxt, flow;
Edge() {}
Edge(int to, int nxt, int flow) : to(to), nxt(nxt), flow(flow) {} } edge[M << 1];
int S, T; int head[N], dep[N], tot;
voidInit(){ memset(head, -1, sizeof head); tot = 0; }
voidset(int _S, int _T){ S = _S, T = _T; }
voidaddedge(int u, int v, int w, int rw = 0){ edge[tot] = Edge(v, head[u], w); head[u] = tot++; edge[tot] = Edge(u, head[v], rw); head[v] = tot++; }
boolBFS(){ memset(dep, -1, sizeof dep); queue<int> q; q.push(S); dep[S] = 1; while (!q.empty()) { int u = q.front(); q.pop(); for (int i = head[u]; ~i; i = edge[i].nxt) { if (edge[i].flow && dep[edge[i].to] == -1) { dep[edge[i].to] = dep[u] + 1; q.push(edge[i].to); } } } return dep[T] > 0; }
intDFS(int u, int f){ if (u == T || f == 0) return f; int w, used = 0; for (int i = head[u]; ~i; i = edge[i].nxt) { if (edge[i].flow && dep[edge[i].to] == dep[u] + 1) { w = DFS(edge[i].to, min(f - used, edge[i].flow)); edge[i].flow -= w; edge[i ^ 1].flow += w; used += w; if (used == f) return f; } } if (!used) dep[u] = -1; return used; }
intgao(){ int res = 0; while (BFS()) { res += DFS(S, INF); } return res; }
voidDel(int u){ for (int i = head[u]; ~i; i = edge[i].nxt) { edge[i].flow = edge[i ^ 1].flow = 0; } } } dicnic;
int n, totFlow[N];
voidRUN(){ dicnic.Init(); memset(totFlow, 0, sizeof totFlow); for (int i = 1, m; i <= n; ++i) { scanf("%d", &m); for (int j = 1, x; j <= m; ++j) { scanf("%d", &x); dicnic.addedge(i, x, INF); totFlow[i]--, totFlow[x]++; } } int S = 0, T = n + 1, SS = n + 2, TT = n + 3; for (int i = 1; i <= n; ++i) { dicnic.addedge(S, i, INF); dicnic.addedge(i, T, INF); } for (int i = 1; i <= n; ++i) { if (totFlow[i] < 0) { dicnic.addedge(i, TT, -totFlow[i]); } else { dicnic.addedge(SS, i, totFlow[i]); } } dicnic.addedge(T, S, INF); dicnic.set(SS, TT); dicnic.gao(); int Max = dicnic.edge[dicnic.tot - 1].flow; dicnic.edge[dicnic.tot - 1].flow = dicnic.edge[dicnic.tot - 2].flow = 0; dicnic.Del(SS), dicnic.Del(TT); dicnic.set(T, S); printf("%d\n", Max - dicnic.gao()); }