提出 #10498510


ソースコード 拡げる

#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;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const 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() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

template <typename T, typename U>
struct PrimalDual {
  using Pui = pair<U, int>;

  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) {}
  };

  vector<vector<Edge> > graph;

  PrimalDual(int n, const T TINF, const U UINF) : n(n), TINF(TINF), UINF(UINF), graph(n), prev_v(n, -1), prev_e(n, -1), potential(n, 0), dist(n) {}

  void add_edge(int src, int dst, T cap, U cost) {
    has_negative_edge |= cost < 0;
    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(int s, int t, T flow) {
    U res = 0;
    if (has_negative_edge) {
      bellman_ford(s);
      if (dist[t] == UINF) return UINF;
      res += calc(s, t, flow);
    }
    while (flow > 0) {
      dijkstra(s);
      if (dist[t] == UINF) return UINF;
      res += calc(s, t, flow);
    }
    return res;
  }

  U minimum_cost_flow(int s, int t) {
    U res = 0;
    bellman_ford(s);
    if (potential[t] >= 0 || dist[t] == UINF) return res;
    T tmp = TINF;
    res += calc(s, t, tmp);
    while (true) {
      dijkstra(s);
      if (potential[t] >= 0 || dist[t] == UINF) return res;
      res += calc(s, t, tmp);
    }
  }

  pair<T, U> min_cost_max_flow(int s, int t, T flow) {
    T mx = flow;
    U cost = 0;
    if (has_negative_edge) {
      bellman_ford(s);
      if (dist[t] == UINF) return {mx - flow, cost};
      cost += calc(s, t, flow);
    }
    while (flow > 0) {
      dijkstra(s);
      if (dist[t] == UINF) return {mx - flow, cost};
      cost += calc(s, t, flow);
    }
    return {mx - flow, cost};
  }

private:
  int n;
  const T TINF;
  const U UINF;
  bool has_negative_edge = false;
  vector<int> prev_v, prev_e;
  vector<U> potential, dist;
  priority_queue<Pui, vector<Pui>, greater<Pui> > que;

  void bellman_ford(int s) {
    fill(ALL(dist), UINF);
    dist[s] = 0;
    bool is_updated = true;
    REP(step, n) {
      is_updated = false;
      REP(i, n) if (dist[i] != UINF) {
        REP(j, graph[i].size()) {
          Edge e = graph[i][j];
          if (e.cap > 0 && dist[e.dst] > dist[i] + e.cost) {
            dist[e.dst] = dist[i] + e.cost;
            prev_v[e.dst] = i;
            prev_e[e.dst] = j;
            is_updated = true;
          }
        }
      }
      if (!is_updated) break;
    }
    assert(!is_updated);
    REP(i, n) {
      if (dist[i] != UINF) potential[i] += dist[i];
    }
  }

  void dijkstra(int s) {
    fill(ALL(dist), UINF);
    dist[s] = 0;
    que.emplace(0, s);
    while (!que.empty()) {
      Pui pr = que.top(); que.pop();
      int ver = pr.second;
      if (dist[ver] < pr.first) continue;
      REP(i, graph[ver].size()) {
        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);
        }
      }
    }
    REP(i, n) {
      if (dist[i] != UINF) potential[i] += dist[i];
    }
  }

  U calc(int s, int t, T &flow) {
    T f = flow;
    for (int v = t; v != s; v = prev_v[v]) f = min(f, graph[prev_v[v]][prev_e[v]].cap);
    flow -= f;
    for (int v = t; v != s; v = prev_v[v]) {
      Edge &e = graph[prev_v[v]][prev_e[v]];
      e.cap -= f;
      graph[v][e.rev].cap += f;
    }
    return potential[t] * f;
  }
};

int main() {
  const ll M = 1000000000;
  int n, m, p, s, t; cin >> n >> m >> p >> s >> t; --s; --t;
  vector<int> v(m), u(m), d(m), c(m); REP(i, m) cin >> v[i] >> u[i] >> d[i] >> c[i], --v[i], --u[i];
  function<pair<double, bool>(double)> f = [&](double y) {
    ll Y = (ll)round(y * M);
    PrimalDual<ll, ll> pd(n, LINF, LINF);
    REP(i, m) pd.add_edge(v[i], u[i], Y * c[i], d[i]);
    ll res = pd.minimum_cost_flow(s, t, M);
    return res == LINF ? make_pair(1.0 * res, false) : make_pair(1.0 * res / M + y * p, true);
  };
  double lb = 0, ub = 1;
  REP(_, 80) {
    double mid1 = (lb + lb + ub) / 3, mid2 = (lb + ub + ub) / 3;
    pair<double, bool> f1 = f(mid1), f2 = f(mid2);
    if (!f1.second) {
      lb = mid1;
    } else if (!f2.second) {
      ub = mid2;
    } else if (f1.first < f2.first) {
      ub = mid2;
    } else {
      lb = mid1;
    }
  }
  cout << f(ub).first << '\n';
  return 0;
}

提出情報

提出日時
問題 J - Longest Shortest Path
ユーザ emthrm
言語 C++14 (GCC 5.4.1)
得点 100
コード長 5424 Byte
結果 AC
実行時間 8749 ms
メモリ 536 KiB

ジャッジ結果

セット名 All
得点 / 配点 100 / 100
結果
AC × 56
セット名 テストケース
All 00_example_00, 00_example_01, 00_example_02, 05_kill_00, 05_kill_01, 05_kill_02, 10_rand_small_000, 10_rand_small_001, 10_rand_small_002, 10_rand_small_003, 10_rand_small_004, 11_rand_medium_000, 11_rand_medium_001, 11_rand_medium_002, 11_rand_medium_003, 11_rand_medium_004, 12_rand_large_000, 12_rand_large_001, 12_rand_large_002, 12_rand_large_003, 12_rand_large_004, 15_rand_small_000, 15_rand_small_001, 15_rand_small_002, 15_rand_small_003, 15_rand_small_004, 20_parallel_small_000, 20_parallel_small_001, 20_parallel_small_002, 20_parallel_small_003, 20_parallel_small_004, 22_parallel_large_000, 22_parallel_large_001, 22_parallel_large_002, 22_parallel_large_003, 22_parallel_large_004, 32_bigDist_large_000, 32_bigDist_large_001, 32_bigDist_large_002, 32_bigDist_large_003, 32_bigDist_large_004, 42_bigCost_large_000, 42_bigCost_large_001, 42_bigCost_large_002, 42_bigCost_large_003, 42_bigCost_large_004, 50_gen_rand_000, 50_gen_rand_001, 50_gen_rand_002, 50_gen_rand_003, 50_gen_rand_004, 52_gen_rand_000, 52_gen_rand_001, 52_gen_rand_002, 52_gen_rand_003, 52_gen_rand_004
ケース名 結果 実行時間 メモリ
00_example_00 AC 2 ms 512 KiB
00_example_01 AC 1 ms 256 KiB
00_example_02 AC 1 ms 256 KiB
05_kill_00 AC 1 ms 256 KiB
05_kill_01 AC 1 ms 256 KiB
05_kill_02 AC 1 ms 256 KiB
10_rand_small_000 AC 1 ms 256 KiB
10_rand_small_001 AC 1 ms 256 KiB
10_rand_small_002 AC 2 ms 256 KiB
10_rand_small_003 AC 1 ms 256 KiB
10_rand_small_004 AC 1 ms 256 KiB
11_rand_medium_000 AC 17 ms 256 KiB
11_rand_medium_001 AC 12 ms 256 KiB
11_rand_medium_002 AC 19 ms 256 KiB
11_rand_medium_003 AC 10 ms 256 KiB
11_rand_medium_004 AC 3 ms 256 KiB
12_rand_large_000 AC 3666 ms 520 KiB
12_rand_large_001 AC 3660 ms 520 KiB
12_rand_large_002 AC 3347 ms 520 KiB
12_rand_large_003 AC 3398 ms 528 KiB
12_rand_large_004 AC 3574 ms 524 KiB
15_rand_small_000 AC 2 ms 256 KiB
15_rand_small_001 AC 2 ms 256 KiB
15_rand_small_002 AC 1 ms 256 KiB
15_rand_small_003 AC 1 ms 256 KiB
15_rand_small_004 AC 1 ms 256 KiB
20_parallel_small_000 AC 1 ms 256 KiB
20_parallel_small_001 AC 1 ms 256 KiB
20_parallel_small_002 AC 1 ms 256 KiB
20_parallel_small_003 AC 1 ms 256 KiB
20_parallel_small_004 AC 1 ms 256 KiB
22_parallel_large_000 AC 8511 ms 536 KiB
22_parallel_large_001 AC 8673 ms 536 KiB
22_parallel_large_002 AC 8749 ms 536 KiB
22_parallel_large_003 AC 5214 ms 536 KiB
22_parallel_large_004 AC 8603 ms 532 KiB
32_bigDist_large_000 AC 1729 ms 524 KiB
32_bigDist_large_001 AC 2137 ms 524 KiB
32_bigDist_large_002 AC 2082 ms 524 KiB
32_bigDist_large_003 AC 2228 ms 528 KiB
32_bigDist_large_004 AC 2077 ms 524 KiB
42_bigCost_large_000 AC 7308 ms 536 KiB
42_bigCost_large_001 AC 7037 ms 536 KiB
42_bigCost_large_002 AC 7633 ms 528 KiB
42_bigCost_large_003 AC 7613 ms 536 KiB
42_bigCost_large_004 AC 7642 ms 536 KiB
50_gen_rand_000 AC 2 ms 256 KiB
50_gen_rand_001 AC 2 ms 256 KiB
50_gen_rand_002 AC 2 ms 256 KiB
50_gen_rand_003 AC 2 ms 256 KiB
50_gen_rand_004 AC 2 ms 256 KiB
52_gen_rand_000 AC 3428 ms 528 KiB
52_gen_rand_001 AC 1654 ms 536 KiB
52_gen_rand_002 AC 3074 ms 528 KiB
52_gen_rand_003 AC 3282 ms 532 KiB
52_gen_rand_004 AC 3580 ms 520 KiB