結果

問題 No.1301 Strange Graph Shortest Path
ユーザー hanyuhanyu
提出日時 2020-11-28 00:23:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 284 ms / 3,000 ms
コード長 2,145 bytes
コンパイル時間 2,371 ms
コンパイル使用メモリ 212,588 KB
実行使用メモリ 42,484 KB
最終ジャッジ日時 2023-10-09 22:55:29
合計ジャッジ時間 12,057 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 241 ms
41,408 KB
testcase_03 AC 198 ms
37,076 KB
testcase_04 AC 276 ms
38,728 KB
testcase_05 AC 212 ms
41,248 KB
testcase_06 AC 246 ms
36,084 KB
testcase_07 AC 237 ms
38,528 KB
testcase_08 AC 204 ms
37,404 KB
testcase_09 AC 238 ms
34,064 KB
testcase_10 AC 208 ms
36,932 KB
testcase_11 AC 256 ms
37,500 KB
testcase_12 AC 258 ms
37,284 KB
testcase_13 AC 243 ms
41,596 KB
testcase_14 AC 234 ms
34,540 KB
testcase_15 AC 234 ms
36,140 KB
testcase_16 AC 284 ms
38,500 KB
testcase_17 AC 256 ms
41,560 KB
testcase_18 AC 228 ms
37,796 KB
testcase_19 AC 254 ms
36,380 KB
testcase_20 AC 250 ms
34,908 KB
testcase_21 AC 249 ms
39,788 KB
testcase_22 AC 251 ms
35,672 KB
testcase_23 AC 246 ms
41,244 KB
testcase_24 AC 253 ms
35,784 KB
testcase_25 AC 278 ms
38,772 KB
testcase_26 AC 251 ms
37,568 KB
testcase_27 AC 249 ms
37,448 KB
testcase_28 AC 219 ms
41,040 KB
testcase_29 AC 270 ms
37,648 KB
testcase_30 AC 259 ms
38,880 KB
testcase_31 AC 259 ms
38,076 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 153 ms
31,568 KB
testcase_34 AC 261 ms
42,484 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
static const ll INF = 1e18;

// 最小費用流
struct edge {
  int to;
  ll cap, cost;
  int rev;
};

vector<ll> h, d; // ポテンシャル, 最短距離
vector<int> prevv, preve; // 直前の頂点と辺

void add_edge(vector<vector<edge>> &G, int from, int to, ll cap, ll cost) {
  G[from].emplace_back((edge) {to, cap, cost, (int) G[to].size()});
  G[to].emplace_back((edge) {from, 0, -cost, (int) G[from].size() - 1});
}

// sからtへの流量fの最小費用流(流せない場合は-1)
ll min_cost_flow(vector<vector<edge>> &G, int s, int t, ll f) {
  ll res = 0;
  h.assign(G.size(), 0);
  prevv.assign(G.size(), 0);
  preve.assign(G.size(), 0);
  while (f > 0) {
    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
    d.assign(G.size(), INF);
    d[s] = 0;
    pq.push(make_pair(0, s));
    while (!pq.empty()) {
      pair<ll, int> p = pq.top();
      pq.pop();
      int v = p.second;
      if (d[v] < p.first) continue;
      for (int i = 0; i < G[v].size(); i++) {
        edge &e = G[v][i];
        if (e.cap > 0 && d[e.to] > d[v] + e.cost + h[v] - h[e.to]) {
          d[e.to] = d[v] + e.cost + h[v] - h[e.to];
          prevv[e.to] = v;
          preve[e.to] = i;
          pq.push(make_pair(d[e.to], e.to));
        }
      }
    }
    if (d[t] == INF) return -1;
    for (int i = 0; i < G.size(); i++) h[i] += d[i];
    
    // s-t間最短路に流す
    int nowf = f;
    for (int i = t; i != s; i = prevv[i]) {
      nowf = min<ll>(nowf, G[prevv[i]][preve[i]].cap);
    }
    f -= nowf;
    res += nowf * h[t];
    for (int i = t; i != s; i = prevv[i]) {
      edge &e = G[prevv[i]][preve[i]];
      e.cap -= nowf;
      G[i][e.rev].cap += nowf;
    }
  }
  return res;
}

int main() {
  ll n, m;
  cin >> n >> m;
  
  vector<vector<edge>> G(n);
  for (int i = 0; i < m; i++) {
    ll u, v, c, d;
    cin >> u >> v >> c >> d;
    u--;
    v--;
    add_edge(G, u, v, 1, c);
    add_edge(G, u, v, 1, d);
    add_edge(G, v, u, 1, c);
    add_edge(G, v, u, 1, d);
  }
  
  cout << min_cost_flow(G, 0, n - 1, 2) << '\n';
}
0