結果

問題 No.1301 Strange Graph Shortest Path
ユーザー 👑 emthrmemthrm
提出日時 2020-11-27 23:52:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 198 ms / 3,000 ms
コード長 3,726 bytes
コンパイル時間 2,614 ms
コンパイル使用メモリ 218,112 KB
実行使用メモリ 34,744 KB
最終ジャッジ日時 2024-07-26 20:13:21
合計ジャッジ時間 9,535 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 155 ms
32,672 KB
testcase_03 AC 126 ms
29,184 KB
testcase_04 AC 197 ms
31,872 KB
testcase_05 AC 126 ms
32,128 KB
testcase_06 AC 168 ms
29,440 KB
testcase_07 AC 159 ms
30,592 KB
testcase_08 AC 126 ms
29,312 KB
testcase_09 AC 165 ms
28,032 KB
testcase_10 AC 125 ms
29,056 KB
testcase_11 AC 175 ms
30,336 KB
testcase_12 AC 176 ms
30,336 KB
testcase_13 AC 153 ms
32,956 KB
testcase_14 AC 158 ms
28,160 KB
testcase_15 AC 158 ms
28,928 KB
testcase_16 AC 197 ms
31,744 KB
testcase_17 AC 166 ms
33,216 KB
testcase_18 AC 148 ms
30,208 KB
testcase_19 AC 179 ms
29,696 KB
testcase_20 AC 182 ms
28,800 KB
testcase_21 AC 166 ms
31,616 KB
testcase_22 AC 186 ms
29,696 KB
testcase_23 AC 161 ms
32,512 KB
testcase_24 AC 185 ms
29,440 KB
testcase_25 AC 192 ms
31,744 KB
testcase_26 AC 163 ms
30,336 KB
testcase_27 AC 172 ms
30,464 KB
testcase_28 AC 135 ms
32,000 KB
testcase_29 AC 198 ms
31,104 KB
testcase_30 AC 183 ms
31,360 KB
testcase_31 AC 187 ms
31,360 KB
testcase_32 AC 2 ms
6,940 KB
testcase_33 AC 80 ms
25,728 KB
testcase_34 AC 183 ms
34,744 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 PrimalDual2 {
  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) {}
  };

  std::vector<std::vector<Edge>> graph;

  PrimalDual2(int n, const T TINF, const U UINF) : n(n), UINF(UINF), graph(n + 2), d(n + 2, 0) {}

  void add_edge(int src, int dst, T cap, U cost) {
    if (cost < 0) {
      d[src] -= cap;
      d[dst] += cap;
      res += cost * cap;
      std::swap(src, dst);
      cost = -cost;
    }
    graph[src].emplace_back(dst, cap, cost, graph[dst].size());
    graph[dst].emplace_back(src, 0, -cost, graph[src].size() - 1);
  }

  U minimum_cost_flow() {
    T flow = 0;
    for (int i = 0; i < n; ++i) {
      if (d[i] > 0) {
        add_edge(n, i, d[i], 0);
        flow += d[i];
      } else if (d[i] < 0) {
        add_edge(i, n + 1, -d[i], 0);
      }
    }
    std::vector<int> prev_v(n + 2, -1), prev_e(n + 2, -1);
    std::vector<U> potential(n + 2, 0), dist(n + 2);
    std::priority_queue<Pui, std::vector<Pui>, std::greater<Pui>> que;
    while (flow > 0) {
      std::fill(dist.begin(), dist.end(), UINF);
      dist[n] = 0;
      que.emplace(0, n);
      while (!que.empty()) {
        U fst; int ver; std::tie(fst, ver) = que.top(); que.pop();
        if (dist[ver] < fst) continue;
        for (int i = 0; i < graph[ver].size(); ++i) {
          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);
          }
        }
      }
      if (dist[n + 1] == UINF) return UINF;
      for (int i = 0; i < n + 2; ++i) {
        if (dist[i] != UINF) potential[i] += dist[i];
      }
      T f = flow;
      for (int v = n + 1; v != n; v = prev_v[v]) {
        if (graph[prev_v[v]][prev_e[v]].cap < f) f = graph[prev_v[v]][prev_e[v]].cap;
      }
      flow -= f;
      res += potential[n + 1] * f;
      for (int v = n + 1; v != n; v = prev_v[v]) {
        Edge &e = graph[prev_v[v]][prev_e[v]];
        e.cap -= f;
        graph[v][e.rev].cap += f;
      }
    }
    return res;
  }

  U minimum_cost_flow(int s, int t, T flow) {
    d[s] += flow;
    d[t] -= flow;
    return minimum_cost_flow();
  }

private:
  using Pui = std::pair<U, int>;

  int n;
  const U UINF;
  U res = 0;
  std::vector<T> d;
};

int main() {
  int n, m; cin >> n >> m;
  PrimalDual2<int, ll> pd(n, INF, LINF);
  while (m--) {
    int u, v, c, d; cin >> u >> v >> c >> d; --u; --v;
    pd.add_edge(u, v, 1, c);
    pd.add_edge(v, u, 1, c);
    pd.add_edge(u, v, 1, d);
    pd.add_edge(v, u, 1, d);
  }
  cout << pd.minimum_cost_flow(0, n - 1, 2) << '\n';
  return 0;
}
0