結果
問題 | No.119 旅行のツアーの問題 |
ユーザー |
|
提出日時 | 2024-09-22 16:51:23 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 1,465 bytes |
コンパイル時間 | 1,530 ms |
コンパイル使用メモリ | 172,404 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-09-22 16:51:26 |
合計ジャッジ時間 | 2,430 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 19 |
ソースコード
#include <bits/stdc++.h>typedef long long LL;const int N = 40 + 7;const int P = 100 + 7;const int E = 10000 + 7;const int S = P - 1;const int T = P - 2;const int INF = 0x3f3f3f3f;int head[P], cur[P], nxt[E], to[E], fl[E], tot = 1;int n, m, ans;int d[P];void add(int x, int y, int z, int w = 0) {nxt[++tot] = head[x], head[x] = tot, to[tot] = y, fl[tot] = z;nxt[++tot] = head[y], head[y] = tot, to[tot] = x, fl[tot] = w;}bool bfs() {std::queue<int> q;memset(d, -1, sizeof d);d[S] = 0, q.push(S);while(!q.empty()) {int x = q.front();q.pop();for(int i = head[x]; i; i = nxt[i])if(d[to[i]] == -1 && fl[i]) {d[to[i]] = d[x] + 1;q.push(to[i]);}}return d[T] != -1;}int dinic(int x, int y) {if(x == T)return y;int f = 0, t;for(int &i = cur[x]; i; i = nxt[i])if(d[to[i]] == d[x] + 1 && fl[i]) {t = dinic(to[i], std::min(fl[i], y - f));f += t;fl[i] -= t;fl[i ^ 1] += t;if(f == y)return y;}return f;}int main() {scanf("%d", &n);for(int i = 1, x, y; i <= n; ++i) {scanf("%d%d", &x, &y);ans += x + y;add(S, i, x);add(i, i + n, x + y);add(i + n, T, y);}scanf("%d", &m);for(int i = 1, x, y; i <= m; ++i) {scanf("%d%d", &x, &y);add(x + 1 + n, y + 1, INF);}while(bfs()) {memcpy(cur, head, sizeof cur);ans -= dinic(S, INF);}printf("%d\n", ans);return 0;}