結果

問題 No.119 旅行のツアーの問題
ユーザー kimiyukikimiyuki
提出日時 2016-01-15 01:13:00
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,659 bytes
コンパイル時間 628 ms
コンパイル使用メモリ 70,884 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-23 09:21:16
合計ジャッジ時間 1,619 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0