結果

問題 No.1069 電柱 / Pole (Hard)
ユーザー risujirohrisujiroh
提出日時 2020-05-29 23:43:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 422 ms / 2,000 ms
コード長 3,956 bytes
コンパイル時間 3,439 ms
コンパイル使用メモリ 241,544 KB
実行使用メモリ 6,664 KB
最終ジャッジ日時 2023-09-30 21:19:07
合計ジャッジ時間 6,449 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 251 ms
6,664 KB
testcase_05 AC 422 ms
5,104 KB
testcase_06 AC 7 ms
4,380 KB
testcase_07 AC 17 ms
4,380 KB
testcase_08 AC 6 ms
4,376 KB
testcase_09 AC 8 ms
4,376 KB
testcase_10 AC 5 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 11 ms
4,380 KB
testcase_14 AC 9 ms
4,376 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 5 ms
4,380 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 5 ms
4,376 KB
testcase_19 AC 7 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 9 ms
4,376 KB
testcase_22 AC 5 ms
4,380 KB
testcase_23 AC 69 ms
4,380 KB
testcase_24 AC 9 ms
4,376 KB
testcase_25 AC 9 ms
4,376 KB
testcase_26 AC 8 ms
4,380 KB
testcase_27 AC 7 ms
4,380 KB
testcase_28 AC 7 ms
4,376 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 3 ms
4,380 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 3 ms
4,376 KB
testcase_42 AC 3 ms
4,376 KB
testcase_43 AC 4 ms
4,376 KB
testcase_44 AC 6 ms
4,380 KB
testcase_45 AC 4 ms
4,380 KB
testcase_46 AC 6 ms
4,380 KB
testcase_47 AC 7 ms
4,376 KB
testcase_48 AC 9 ms
4,380 KB
testcase_49 AC 5 ms
4,376 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 6 ms
4,376 KB
testcase_52 AC 3 ms
4,380 KB
testcase_53 AC 6 ms
4,380 KB
testcase_54 AC 1 ms
4,380 KB
testcase_55 AC 2 ms
4,376 KB
testcase_56 AC 2 ms
4,380 KB
testcase_57 AC 2 ms
4,380 KB
testcase_58 AC 2 ms
4,380 KB
testcase_59 AC 2 ms
4,376 KB
testcase_60 AC 1 ms
4,384 KB
testcase_61 AC 2 ms
4,380 KB
testcase_62 AC 2 ms
4,376 KB
testcase_63 AC 1 ms
4,376 KB
testcase_64 AC 2 ms
4,376 KB
testcase_65 AC 2 ms
4,380 KB
testcase_66 AC 2 ms
4,380 KB
testcase_67 AC 1 ms
4,376 KB
testcase_68 AC 2 ms
4,376 KB
testcase_69 AC 3 ms
4,380 KB
testcase_70 AC 3 ms
4,380 KB
testcase_71 AC 3 ms
4,376 KB
testcase_72 AC 2 ms
4,376 KB
testcase_73 AC 3 ms
4,376 KB
testcase_74 AC 3 ms
4,380 KB
testcase_75 AC 3 ms
4,380 KB
testcase_76 AC 9 ms
4,380 KB
testcase_77 AC 13 ms
4,380 KB
testcase_78 AC 2 ms
4,376 KB
testcase_79 AC 10 ms
4,376 KB
testcase_80 AC 8 ms
4,380 KB
testcase_81 AC 5 ms
4,376 KB
testcase_82 AC 4 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#ifdef LOCAL
#include "debug.h"
#else
#define DEBUG(...)
#endif

template <class T> struct graph {
  struct edge {
    int to;
    T w;
    operator int() const { return to; }
  };
  template <class It> struct edge_list {
    const It bg, ed;
    It begin() const { return bg; }
    It end() const { return ed; }
    auto&& operator[](size_t i) const { return bg[i]; }
    size_t size() const { return ed - bg; }
  };
  vector<pair<int, edge>> pool;
  vector<int> head;
  vector<edge> edges;
  graph(int n) : head(n + 1) {}
  void add(int from, int to, T w = 1) {
    pool.push_back({from, {to, w}});
    ++head[from];
  }
  void build() {
    partial_sum(begin(head), end(head), begin(head));
    edges.resize(head.back());
    while (not empty(pool)) {
      edges[--head[pool.back().first]] = pool.back().second;
      pool.pop_back();
    }
    decltype(pool)().swap(pool);
  }
  edge_list<class vector<edge>::const_iterator> operator[](int v) const {
    return {begin(edges) + head[v], begin(edges) + head[v + 1]};
  }
  edge_list<class vector<edge>::iterator> operator[](int v) {
    return {begin(edges) + head[v], begin(edges) + head[v + 1]};
  }
  size_t size() const { return std::size(head) - 1; }
};

template <class T> constexpr T inf = numeric_limits<T>::max() / 2.1;

auto chmin = [](auto&& a, auto b) { return b < a ? a = b, 1 : 0; };

template <class T, class G> auto dijkstra(const G& g, int s, int t) {
  vector d(size(g), inf<T>);
  vector prv(size(g), -1);
  priority_queue<pair<T, int>, vector<pair<T, int>>, greater<>> rh;
  rh.emplace(d[s] = 0, s);
  while (not empty(rh)) {
    auto [dv, v] = rh.top();
    rh.pop();
    if (dv != d[v]) continue;
    for (auto&& e : g[v])
      if (chmin(d[e.to], dv + e.w)) {
        rh.emplace(d[e.to], e.to);
        prv[e.to] = v;
      }
  }
  vector<int> vs;
  for (int v = t; v != -1; v = prv[v]) {
    vs.push_back(v);
    if (v == s) break;
  }
  reverse(begin(vs), end(vs));
  return make_pair(d[t], vs);
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  int n, m, k, s, t;
  cin >> n >> m >> k >> s >> t;
  --s, --t;
  vector<double> x(n), y(n);
  for (int i = 0; i < n; ++i) {
    cin >> x[i] >> y[i];
  }
  graph<double> g(n);
  while (m--) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    double w = hypot(x[v] - x[u], y[v] - y[u]);
    g.add(u, v, w);
    g.add(v, u, w);
  }
  g.build();
  set<pair<double, vector<int>>> se;
  se.insert(dijkstra<double>(g, s, t));
  vector<vector<int>> ps;
  while (k--) {
    if (empty(se)) {
      cout << "-1\n";
      continue;
    }
    cout << begin(se)->first << '\n';
    auto vs = begin(se)->second;
    ps.push_back(vs);
    se.erase(begin(se));
    double cur = 0;
    for (int i = 0; i < (int)size(vs) - 1; ++i) {
      int v = vs[i];
      vector<map<int, double>> mp(n);
      for (auto&& p : ps) {
        if (i < (int)size(p) and vector(begin(p), begin(p) + (i + 1)) == vector(begin(vs), begin(vs) + (i + 1))) {
          for (auto&& e : g[v]) {
            if (e == p[i + 1] and e.w != inf<double>) {
              mp[v][e] = exchange(e.w, inf<double>);
            }
          }
        }
      }
      for (int j = 0; j < i; ++j) {
        for (auto&& e : g[vs[j]]) {
          mp[vs[j]][e] = exchange(e.w, inf<double>);
        }
      }
      auto p = dijkstra<double>(g, v, t);
      if (p.first < inf<double>) {
        vector nvs(begin(vs), begin(vs) + i);
        nvs.insert(end(nvs), begin(p.second), end(p.second));
        se.emplace(cur + p.first, nvs);
      }
      for (int j = 0; j < i; ++j) {
        for (auto&& e : g[vs[j]]) {
          if (e.w == inf<double>) {
            e.w = mp[vs[j]][e];
          }
        }
      }
      for (auto&& e : g[v]) {
        if (mp[v].count(e)) {
          e.w = mp[v][e];
        }
        if (e == vs[i + 1]) {
          cur += e.w;
        }
      }
    }
  }
}
0