結果
| 問題 |
No.119 旅行のツアーの問題
|
| コンテスト | |
| ユーザー |
msm1993
|
| 提出日時 | 2020-06-02 09:51:05 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 4 ms / 5,000 ms |
| コード長 | 1,386 bytes |
| コンパイル時間 | 1,212 ms |
| コンパイル使用メモリ | 82,296 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-23 18:21:55 |
| 合計ジャッジ時間 | 1,929 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 19 |
ソースコード
#include <iostream>
#include <vector>
using namespace std;
struct edge { int to, avail, rev; };
constexpr int inf = 987'654'321;
int N, n;
vector<vector<edge>> G;
vector<int> ptr;
vector<bool> used;
void add_edge(int u, int v, int c) {
G[u].push_back(edge{v, c, ptr[v]++});
G[v].push_back(edge{u, 0, ptr[u]++});
}
int dfs(int v, int t, int flow) {
if(v == t) { return flow; }
used[v] = true;
for(edge &e : G[v]) {
edge &x = G[e.to][e.rev];
if(!used[e.to] && e.avail > 0) {
int res = dfs(e.to, t, min(e.avail, flow));
if(res > 0) {
e.avail -= res;
x.avail += res;
return res;
}
}
}
return 0;
}
int max_flow(int s, int t) {
int res = 0;
for(;;) {
used.assign(n, false);
int flow = dfs(s, t, inf);
if(flow == 0) { return res; }
res += flow;
}
}
int main(void) {
scanf("%d", &N);
::n = 2 * N + 2;
int src = 2 * N, dst = 2 * N + 1;
int acc = 0;
G.assign(n, vector<edge>());
ptr.assign(n, 0);
for(int i=0; i<N; ++i) {
int B, C; scanf("%d%d", &B, &C);
acc += B + C;
add_edge(src, i, B);
add_edge(i+N, dst, C);
add_edge(i, i+N, inf);
}
int M; scanf("%d", &M);
for(int i=0; i<M; ++i) {
int D, E; scanf("%d%d", &D, &E);
add_edge(D, E+N, inf);
}
int min_cut = max_flow(src, dst);
int res = acc - min_cut;
printf("%d\n", res);
return 0;
}
msm1993