結果

問題 No.1301 Strange Graph Shortest Path
ユーザー m_tsubasam_tsubasa
提出日時 2020-11-27 22:31:27
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 492 ms / 3,000 ms
コード長 3,351 bytes
コンパイル時間 2,828 ms
コンパイル使用メモリ 230,404 KB
実行使用メモリ 73,516 KB
最終ジャッジ日時 2023-10-09 21:33:20
合計ジャッジ時間 18,932 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 458 ms
66,544 KB
testcase_03 AC 390 ms
58,468 KB
testcase_04 AC 485 ms
71,220 KB
testcase_05 AC 456 ms
64,456 KB
testcase_06 AC 429 ms
65,312 KB
testcase_07 AC 434 ms
64,820 KB
testcase_08 AC 393 ms
58,932 KB
testcase_09 AC 404 ms
62,320 KB
testcase_10 AC 379 ms
58,956 KB
testcase_11 AC 437 ms
66,816 KB
testcase_12 AC 425 ms
67,444 KB
testcase_13 AC 447 ms
66,836 KB
testcase_14 AC 407 ms
61,856 KB
testcase_15 AC 422 ms
62,160 KB
testcase_16 AC 492 ms
71,392 KB
testcase_17 AC 475 ms
68,800 KB
testcase_18 AC 423 ms
62,044 KB
testcase_19 AC 447 ms
65,932 KB
testcase_20 AC 436 ms
65,680 KB
testcase_21 AC 453 ms
66,872 KB
testcase_22 AC 429 ms
67,620 KB
testcase_23 AC 438 ms
67,124 KB
testcase_24 AC 432 ms
66,224 KB
testcase_25 AC 461 ms
70,116 KB
testcase_26 AC 433 ms
65,176 KB
testcase_27 AC 440 ms
66,640 KB
testcase_28 AC 427 ms
63,192 KB
testcase_29 AC 467 ms
71,600 KB
testcase_30 AC 477 ms
69,456 KB
testcase_31 AC 478 ms
69,600 KB
testcase_32 AC 1 ms
4,352 KB
testcase_33 AC 211 ms
61,632 KB
testcase_34 AC 488 ms
73,516 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define inf (long long)(1e17)
using namespace std;

template <class T>
struct Primal_Dual {
  using Pa = pair<T, int>;
  long long infinity = inf;
  struct edge {
    int to;
    T cap, cost;
    int rev;
  };
  int v;
  vector<vector<edge>> edges;
  vector<T> h;
  vector<T> dist;
  vector<int> prevv, preve;
  Primal_Dual(int vsize = 1) {
    v = vsize;
    edges.resize(v);
    h.resize(v);
    dist.resize(v);
    prevv.resize(v);
    preve.resize(v);
  }
  bool add(int from, int to, T cap, T cost) {
    edges[from].push_back((edge){to, cap, cost, (int)edges[to].size()});
    edges[to].push_back((edge){from, 0, -cost, (int)edges[from].size() - 1});
    return 1;
  }
  T solve(int s, int t, T f) {
    T ans = 0;
    h.assign(v, 0);
    while (f > 0) {
      priority_queue<Pa, vector<Pa>, greater<Pa>> qu;
      dist.assign(v, infinity);
      dist[s] = 0;
      qu.push({0, s});
      while (!qu.empty()) {
        Pa now = qu.top();
        qu.pop();
        int nowv = now.second;
        if (dist[nowv] < now.first) continue;
        for (int i = 0; i < (int)edges[nowv].size(); ++i) {
          edge &e = edges[nowv][i];
          if (e.cap > 0 &&
              dist[e.to] > dist[nowv] + e.cost + h[nowv] - h[e.to]) {
            dist[e.to] = dist[nowv] + e.cost + h[nowv] - h[e.to];
            prevv[e.to] = nowv;
            preve[e.to] = i;
            qu.push({dist[e.to], e.to});
          }
        }
      }
      if (dist[t] == infinity) return -1;
      for (int i = 0; i < v; ++i) h[i] += dist[i];
      T d = f;
      for (int i = t; i != s; i = prevv[i])
        d = min(d, edges[prevv[i]][preve[i]].cap);
      f -= d;
      ans += d * h[t];
      for (int i = t; i != s; i = prevv[i]) {
        edge &e = edges[prevv[i]][preve[i]];
        e.cap -= d;
        edges[i][e.rev].cap += d;
      }
    }
    return ans;
  }
};

using P = pair<long long, long long>;

int n, m;
vector<map<int, long long>> g, g2;
priority_queue<P, vector<P>, greater<P>> pq;
Primal_Dual<long long> pd;

long long solve();

int main() {
  cin >> n >> m;
  g.resize(n);
  g2.resize(n);
  pd = Primal_Dual<long long>(n);
  for (int i = 0; i < m; ++i) {
    int x, y, c, d;
    cin >> x >> y >> c >> d;
    --x, --y;
    pd.add(x, y, 1, c);
    pd.add(y, x, 1, c);
    pd.add(x, y, 1, d);
    pd.add(y, x, 1, d);
    g[x][y] = g[y][x] = c;
    g2[x][y] = g2[y][x] = d;
  }
  cout << solve() << endl;
  return 0;
}

long long solve() {
  return pd.solve(0, n - 1, 2);
  long long res = 0;
  auto dijk = [](vector<map<int, long long>> &g, vector<long long> &dist,
                 vector<long long> &par) {
    dist.assign(n, inf);
    par.assign(n, -1);
    pq.push(P(0, 0));
    dist[0] = 0;
    while (pq.size()) {
      auto [d, now] = pq.top();
      pq.pop();
      if (d != dist[now]) continue;
      for (auto [to, cost] : g[now])
        if (d + cost < dist[to]) {
          dist[to] = d + cost;
          par[to] = now;
          pq.push(P(dist[to], to));
        }
    }
    return dist[n - 1];
  };
  vector<long long> dist, par;
  res += dijk(g, dist, par);
  int now = n - 1;
  while (now != 0) {
    int to = par[now];
    g[to][now] = g[now][to] = -1;
    now = to;
  }
  for (int i = 0; i < n; ++i)
    for (auto [to, cost] : g[i])
      if (cost >= 0) g2[i][to] = cost;
  return res + dijk(g2, dist, par);
}
0