結果

問題 No.1301 Strange Graph Shortest Path
ユーザー m_tsubasam_tsubasa
提出日時 2020-11-27 22:35:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 284 ms / 3,000 ms
コード長 2,259 bytes
コンパイル時間 2,702 ms
コンパイル使用メモリ 215,416 KB
実行使用メモリ 43,136 KB
最終ジャッジ日時 2023-10-09 21:35:47
合計ジャッジ時間 12,531 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 240 ms
41,256 KB
testcase_03 AC 203 ms
37,048 KB
testcase_04 AC 283 ms
38,692 KB
testcase_05 AC 207 ms
41,388 KB
testcase_06 AC 247 ms
36,196 KB
testcase_07 AC 240 ms
38,576 KB
testcase_08 AC 196 ms
37,404 KB
testcase_09 AC 235 ms
34,072 KB
testcase_10 AC 203 ms
36,896 KB
testcase_11 AC 256 ms
37,356 KB
testcase_12 AC 262 ms
37,136 KB
testcase_13 AC 241 ms
41,704 KB
testcase_14 AC 243 ms
34,708 KB
testcase_15 AC 241 ms
36,112 KB
testcase_16 AC 284 ms
38,744 KB
testcase_17 AC 250 ms
41,628 KB
testcase_18 AC 228 ms
37,676 KB
testcase_19 AC 262 ms
36,212 KB
testcase_20 AC 253 ms
34,996 KB
testcase_21 AC 245 ms
39,952 KB
testcase_22 AC 258 ms
35,756 KB
testcase_23 AC 237 ms
41,432 KB
testcase_24 AC 258 ms
35,732 KB
testcase_25 AC 277 ms
38,884 KB
testcase_26 AC 253 ms
37,656 KB
testcase_27 AC 256 ms
37,524 KB
testcase_28 AC 216 ms
41,176 KB
testcase_29 AC 275 ms
37,696 KB
testcase_30 AC 261 ms
38,856 KB
testcase_31 AC 264 ms
38,072 KB
testcase_32 AC 1 ms
4,352 KB
testcase_33 AC 152 ms
31,556 KB
testcase_34 AC 250 ms
43,136 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;
Primal_Dual<long long> pd;

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