結果

問題 No.119 旅行のツアーの問題
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2017-05-09 17:29:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,838 bytes
コンパイル時間 1,917 ms
コンパイル使用メモリ 177,820 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-23 09:23:03
合計ジャッジ時間 2,874 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
//---------------------------------------------------------------------------------------------------
template<class V> struct MaxFlow {
    struct edge { int to, reve; V cap; edge(int t, int r, V c) : to(t), reve(r), cap(c) {} };
    int MV; vector<vector<edge>> E; vector<int> itr, lev;
    MaxFlow() {} MaxFlow(int n) { init(n); }
    void init(int n) { MV = n; itr = vector<int>(MV), lev = vector<int>(MV); E = vector<vector<edge>>(MV); }
    void add_edge(int x, int y, V cap, bool undir = false) { E[x].push_back(edge(y, (int)E[y].size(), cap));
        E[y].push_back(edge(x, (int)E[x].size() - 1, undir ? cap : 0)); }
    void bfs(int cur) { rep(i, 0, MV) lev[i] = -1; queue<int> q; lev[cur] = 0; q.push(cur);
        while (q.size()) { int v = q.front(); q.pop();
        for(auto e : E[v]) if (e.cap>0 && lev[e.to]<0) lev[e.to] = lev[v] + 1, q.push(e.to); } }
    V dfs(int from, int to, V cf) { if (from == to) return cf;
        for (; itr[from]<E[from].size(); itr[from]++) {
            edge* e = &E[from][itr[from]]; if (e->cap>0 && lev[from]<lev[e->to]) { 
                V f = dfs(e->to, to, min(cf, e->cap));
                if (f>0) { e->cap -= f; E[e->to][e->reve].cap += f; return f; }
            } } return 0; }
    V maxflow(int from, int to) { V fl = 0, tf;
        while (1) { bfs(from); if (lev[to]<0) return fl;
            rep(i, 0, MV) itr[i] = 0; while ((tf = dfs(from, to, numeric_limits<V>::max()))>0) fl += tf; } }
};
/*---------------------------------------------------------------------------------------------------
            ∧_∧  
      ∧_∧  (´<_` )  Welcome to My Coding Space
     ( ´_ゝ`) /  ⌒i  
    /   \    | |  
    /   / ̄ ̄ ̄ ̄/  |  
  __(__ニつ/     _/ .| .|____  
     \/____/ (u ⊃  
---------------------------------------------------------------------------------------------------*/







#define INF 1e9
int N, B[50], C[50], M, D[5010], E[5010];
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N;

    MaxFlow<int> mf(N * 2 + 2);
    int s = N * 2, t = N * 2 + 1;

    int ans = 0;
    rep(i, 0, N) {
        cin >> B[i] >> C[i];
        int ma = max(B[i], C[i]);
        ans += ma;

        mf.add_edge(s, i, ma - C[i]);
        mf.add_edge(i, i + N, ma);
        mf.add_edge(i + N, t, ma - B[i]);
    }

    cin >> M;
    rep(i, 0, M) {
        cin >> D[i] >> E[i];
        mf.add_edge(D[i] + N, E[i], INF);
    }

    ans -= mf.maxflow(s, t);
    cout << ans << endl;
}
0