結果

問題 No.119 旅行のツアーの問題
ユーザー msm1993msm1993
提出日時 2020-06-02 09:51:05
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,386 bytes
コンパイル時間 1,020 ms
コンパイル使用メモリ 82,376 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-15 11:35:10
合計ジャッジ時間 2,573 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

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