結果

問題 No.1678 Coin Trade (Multiple)
ユーザー 👑 emthrmemthrm
提出日時 2021-09-10 21:53:28
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,069 ms / 5,000 ms
コード長 5,883 bytes
コンパイル時間 2,626 ms
コンパイル使用メモリ 215,000 KB
実行使用メモリ 14,072 KB
最終ジャッジ日時 2023-09-02 17:02:48
合計ジャッジ時間 20,249 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 77 ms
9,472 KB
testcase_04 AC 508 ms
12,088 KB
testcase_05 AC 183 ms
13,564 KB
testcase_06 AC 133 ms
12,928 KB
testcase_07 AC 454 ms
10,112 KB
testcase_08 AC 282 ms
10,672 KB
testcase_09 AC 212 ms
13,692 KB
testcase_10 AC 98 ms
7,152 KB
testcase_11 AC 363 ms
11,620 KB
testcase_12 AC 83 ms
6,692 KB
testcase_13 AC 832 ms
13,124 KB
testcase_14 AC 237 ms
9,348 KB
testcase_15 AC 262 ms
11,432 KB
testcase_16 AC 49 ms
11,656 KB
testcase_17 AC 224 ms
13,780 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,384 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 2 ms
4,384 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 1 ms
4,384 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 1 ms
4,376 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 2 ms
4,376 KB
testcase_43 AC 1 ms
4,376 KB
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 2 ms
4,380 KB
testcase_46 AC 2 ms
4,380 KB
testcase_47 AC 2 ms
4,376 KB
testcase_48 AC 1,065 ms
14,020 KB
testcase_49 AC 1,064 ms
13,996 KB
testcase_50 AC 1,063 ms
14,072 KB
testcase_51 AC 1,069 ms
13,988 KB
testcase_52 AC 1,048 ms
14,060 KB
testcase_53 AC 1,038 ms
13,980 KB
testcase_54 AC 1,046 ms
14,044 KB
testcase_55 AC 1,058 ms
14,048 KB
testcase_56 AC 1,069 ms
14,044 KB
testcase_57 AC 1,047 ms
14,020 KB
testcase_58 AC 387 ms
13,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

template <typename T, typename U>
struct MinimumCostSTFlow {
  struct Edge {
    int dst, rev;
    T cap;
    U cost;
    Edge(int dst, T cap, U cost, int rev) : dst(dst), cap(cap), cost(cost), rev(rev) {}
  };

  const U uinf;
  std::vector<std::vector<Edge>> graph;

  MinimumCostSTFlow(int n, const U uinf = std::numeric_limits<U>::max())
  : n(n), uinf(uinf), graph(n), prev_v(n, -1), prev_e(n, -1), dist(n), potential(n, 0) {}

  void add_edge(int src, int dst, T cap, U cost) {
    has_negative_edge |= cost < 0;
    graph[src].emplace_back(dst, cap, cost, graph[dst].size());
    graph[dst].emplace_back(src, 0, -cost, graph[src].size() - 1);
  }

  U solve(int s, int t, T flow) {
    U res = 0;
    while (flow > 0) {
      if (has_negative_edge) {
        bellman_ford(s);
        has_negative_edge = false;
      } else {
        dijkstra(s);
      }
      if (dist[t] == uinf) return uinf;
      res += calc(s, t, flow);
    }
    return res;
  }

  U solve(int s, int t) {
    U res = 0;
    T f = tinf;
    bool init = false;
    while (true) {
      if (init) {
        dijkstra(s);
      } else {
        bellman_ford(s);
        init = true;
      }
      if (potential[t] >= 0 || dist[t] == uinf) return res;
      res += calc(s, t, f);
    }
  }

  std::pair<T, U> minimum_cost_maximum_flow(int s, int t, T flow) {
    T f = flow;
    U cost = 0;
    while (flow > 0) {
      if (has_negative_edge) {
        bellman_ford(s);
        has_negative_edge = false;
      } else {
        dijkstra(s);
      }
      if (dist[t] == uinf) return {f - flow, cost};
      cost += calc(s, t, flow);
    }
    return {f, cost};
  }

  U abc214_h(int s, int t, T flow, const vector<int> &topol) {
    U res = 0;
    while (flow > 0) {
      if (has_negative_edge) {
        std::fill(dist.begin(), dist.end(), uinf);
        dist[s] = 0;
        for (int i : topol) {
          if (dist[i] == uinf) continue;
          for (int j = 0; j < graph[i].size(); ++j) {
            const Edge &e = graph[i][j];
            if (e.cap > 0 && dist[e.dst] > dist[i] + e.cost) {
              dist[e.dst] = dist[i] + e.cost;
              prev_v[e.dst] = i;
              prev_e[e.dst] = j;
            }
          }
        }
        for (int i = 0; i < n; ++i) {
          if (dist[i] != uinf) potential[i] += dist[i];
        }

        has_negative_edge = false;
      } else {
        dijkstra(s);
      }
      if (dist[t] == uinf) return uinf;
      res += calc(s, t, flow);
    }
    return res;
  }

private:
  const T tinf = std::numeric_limits<T>::max();
  int n;
  bool has_negative_edge = false;
  std::vector<int> prev_v, prev_e;
  std::vector<U> dist, potential;
  std::priority_queue<std::pair<U, int>, std::vector<std::pair<U, int>>, std::greater<std::pair<U, int>>> que;

  void bellman_ford(int s) {
    std::fill(dist.begin(), dist.end(), uinf);
    dist[s] = 0;
    bool is_updated = true;
    for (int step = 0; step < n; ++step) {
      is_updated = false;
      for (int i = 0; i < n; ++i) {
        if (dist[i] == uinf) continue;
        for (int j = 0; j < graph[i].size(); ++j) {
          const Edge &e = graph[i][j];
          if (e.cap > 0 && dist[e.dst] > dist[i] + e.cost) {
            dist[e.dst] = dist[i] + e.cost;
            prev_v[e.dst] = i;
            prev_e[e.dst] = j;
            is_updated = true;
          }
        }
      }
      if (!is_updated) break;
    }
    assert(!is_updated);
    for (int i = 0; i < n; ++i) {
      if (dist[i] != uinf) potential[i] += dist[i];
    }
  }

  void dijkstra(int s) {
    std::fill(dist.begin(), dist.end(), uinf);
    dist[s] = 0;
    que.emplace(0, s);
    while (!que.empty()) {
      std::pair<U, int> pr = que.top(); que.pop();
      int ver = pr.second;
      if (dist[ver] < pr.first) continue;
      for (int i = 0; i < graph[ver].size(); ++i) {
        const Edge &e = graph[ver][i];
        U nx = dist[ver] + e.cost + potential[ver] - potential[e.dst];
        if (e.cap > 0 && dist[e.dst] > nx) {
          dist[e.dst] = nx;
          prev_v[e.dst] = ver;
          prev_e[e.dst] = i;
          que.emplace(dist[e.dst], e.dst);
        }
      }
    }
    for (int i = 0; i < n; ++i) {
      if (dist[i] != uinf) potential[i] += dist[i];
    }
  }

  U calc(int s, int t, T &flow) {
    T f = flow;
    for (int v = t; v != s; v = prev_v[v]) f = std::min(f, graph[prev_v[v]][prev_e[v]].cap);
    flow -= f;
    for (int v = t; v != s; v = prev_v[v]) {
      Edge &e = graph[prev_v[v]][prev_e[v]];
      e.cap -= f;
      graph[v][e.rev].cap += f;
    }
    return potential[t] * f;
  }
};

int main() {
  int n, k; cin >> n >> k;
  vector<int> a(n);
  MinimumCostSTFlow<int, ll> flow(n);
  FOR(i, 1, n) flow.add_edge(i - 1, i, k, 0);
  REP(i, n) {
    int m; cin >> a[i] >> m;
    while (m--) {
      int b; cin >> b; --b;
      flow.add_edge(b, i, 1, a[b] - a[i]);
    }
  }
  vector<int> order(n);
  iota(ALL(order), 0);
  cout << -flow.abc214_h(0, n - 1, k, order) << '\n';
  return 0;
}
0