結果
問題 |
No.119 旅行のツアーの問題
|
ユーザー |
|
提出日時 | 2016-01-15 01:13:00 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 4 ms / 5,000 ms |
コード長 | 1,659 bytes |
コンパイル時間 | 707 ms |
コンパイル使用メモリ | 71,060 KB |
実行使用メモリ | 6,824 KB |
最終ジャッジ日時 | 2024-12-21 10:54:22 |
合計ジャッジ時間 | 1,459 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 19 |
ソースコード
#include <iostream> #include <vector> #include <queue> #define repeat(i,n) for (int i = 0; (i) < (n); ++(i)) typedef long long ll; using namespace std; ll maximum_flow(int s, int t, vector<vector<ll> > const & g /* capacity, adjacency matrix */) { // edmonds karp, O(E^2V) int n = g.size(); vector<vector<ll> > flow(n, vector<ll>(n)); auto residue = [&](int i, int j) { return g[i][j] - flow[i][j]; }; ll result = 0; while (true) { vector<int> prev(n, -1); vector<ll> f(n); // find the shortest augmenting path queue<int> q; // bfs q.push(s); while (not q.empty()) { int i = q.front(); q.pop(); repeat (j,n) if (prev[j] == -1 and j != s and residue(i,j) > 0) { prev[j] = i; f[j] = residue(i,j); if (i != s) f[j] = min(f[j], f[i]); q.push(j); } } if (prev[t] == -1) break; // not found // backtrack for (int i = t; prev[i] != -1; i = prev[i]) { int j = prev[i]; flow[j][i] += f[t]; flow[i][j] -= f[t]; } result += f[t]; } return result; } const ll INF = 1000000007; int main() { int n; cin >> n; ll a = 0; vector<vector<ll> > g(2*n+2, vector<ll>(2*n+2)); repeat (i,n) { ll b, c; cin >> b >> c; a += b + c; g[2*n][ i] = b; g[ i][ n+i] = INF; g[n+i][2*n+1] = c; } int m; cin >> m; repeat (i,m) { int d, e; cin >> d >> e; g[d][n+e] = INF; } cout << a - maximum_flow(2*n, 2*n+1, g) << endl; return 0; }