結果

問題 No.1301 Strange Graph Shortest Path
ユーザー simansiman
提出日時 2022-03-24 18:29:49
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 278 ms / 3,000 ms
コード長 2,863 bytes
コンパイル時間 1,712 ms
コンパイル使用メモリ 144,116 KB
実行使用メモリ 47,308 KB
最終ジャッジ日時 2024-04-21 03:50:57
合計ジャッジ時間 12,672 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
9,728 KB
testcase_01 AC 5 ms
9,728 KB
testcase_02 AC 234 ms
46,672 KB
testcase_03 AC 198 ms
42,524 KB
testcase_04 AC 278 ms
42,368 KB
testcase_05 AC 214 ms
47,048 KB
testcase_06 AC 244 ms
40,220 KB
testcase_07 AC 239 ms
43,404 KB
testcase_08 AC 203 ms
43,072 KB
testcase_09 AC 228 ms
38,272 KB
testcase_10 AC 199 ms
42,436 KB
testcase_11 AC 246 ms
41,652 KB
testcase_12 AC 253 ms
41,276 KB
testcase_13 AC 236 ms
46,728 KB
testcase_14 AC 229 ms
39,148 KB
testcase_15 AC 224 ms
40,752 KB
testcase_16 AC 271 ms
42,236 KB
testcase_17 AC 244 ms
46,672 KB
testcase_18 AC 223 ms
43,040 KB
testcase_19 AC 248 ms
40,320 KB
testcase_20 AC 240 ms
38,528 KB
testcase_21 AC 243 ms
44,848 KB
testcase_22 AC 259 ms
39,284 KB
testcase_23 AC 263 ms
46,344 KB
testcase_24 AC 246 ms
39,680 KB
testcase_25 AC 266 ms
42,868 KB
testcase_26 AC 241 ms
42,188 KB
testcase_27 AC 245 ms
42,096 KB
testcase_28 AC 211 ms
46,680 KB
testcase_29 AC 273 ms
41,184 KB
testcase_30 AC 261 ms
42,940 KB
testcase_31 AC 262 ms
42,052 KB
testcase_32 AC 6 ms
9,728 KB
testcase_33 AC 157 ms
36,608 KB
testcase_34 AC 249 ms
47,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

const int MAX_V = 200010;
const ll INF = LLONG_MAX;

typedef pair<ll, int> P;
int V;

struct Edge {
  int to;
  ll cap;
  ll cost;
  int rev;

  Edge(int to = -1, ll cap = -1, ll cost = -1, int rev = -1) {
    this->to = to;
    this->cap = cap;
    this->cost = cost;
    this->rev = rev;
  }
};

ll h[MAX_V];
ll dist[MAX_V];
int prevv[MAX_V];
int preve[MAX_V];
vector <Edge> G[MAX_V];

class MinCostFlow {
public:
  int V;

  MinCostFlow(int V) {
    this->V = V;
  }

  void add_edge(int from, int to, ll cap, ll cost) {
    G[from].push_back(Edge(to, cap, cost, G[to].size()));
    G[to].push_back(Edge(from, 0, -cost, G[from].size() - 1));
  }

  ll min_cost_flow(int s, int t, ll flow_limit) {
    ll f = 0;
    ll totalCost = 0;
    fill(h, h + MAX_V, 0);

    while (f < flow_limit) {
      // fprintf(stderr, "f: %lld, limit: %lld\n", f, flow_limit);
      priority_queue<P, vector<P>, greater<P>> pque;
      fill(dist, dist + V, INF);
      dist[s] = 0;
      pque.push(P(0, s));

      while (!pque.empty()) {
        P p = pque.top();
        pque.pop();
        int v = p.second;
        if (dist[v] < p.first) continue;

        for (int i = 0; i < (int) G[v].size(); ++i) {
          Edge *edge = &G[v][i];
          if (edge->cap <= 0) continue;

          ll cost = edge->cost + h[v] - h[edge->to];
          if (dist[edge->to] - dist[v] > cost) {
            dist[edge->to] = dist[v] + cost;
            prevv[edge->to] = v;
            preve[edge->to] = i;
            pque.push(P(dist[edge->to], edge->to));
          }
        }
      }

      if (dist[t] == INF) {
        return -1;
      }

      for (int v = 0; v < V; ++v) {
        h[v] += dist[v];
      }

      ll c = flow_limit - f;
      for (int v = t; v != s; v = prevv[v]) {
        c = min(c, G[prevv[v]][preve[v]].cap);
      }

      f += c;
      totalCost += c * h[t];

      // fprintf(stderr, "h[s]: %d, h[t]: %d, cost: %d, prev_cost: %d\n", h[s], h[t], cost, prev_cost);

      for (int v = t; v != s; v = prevv[v]) {
        Edge *edge = &G[prevv[v]][preve[v]];
        edge->cap -= c;
        G[v][edge->rev].cap += c;
      }
    }

    return totalCost;
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  V = N + 3;

  MinCostFlow mcf(V);
  int u, v;
  ll c, d;

  for (int i = 0; i < M; ++i) {
    cin >> u >> v >> c >> d;
    mcf.add_edge(u, v, 1, c);
    mcf.add_edge(v, u, 1, c);
    mcf.add_edge(u, v, 1, d);
    mcf.add_edge(v, u, 1, d);
  }

  int s = N + 1;
  int t = N + 2;
  mcf.add_edge(s, 1, 2, 0);
  mcf.add_edge(N, t, 2, 0);

  ll res = mcf.min_cost_flow(s, t, 2);

  cout << res << endl;

  return 0;
}
0