結果

問題 No.807 umg tours
ユーザー risujirohrisujiroh
提出日時 2019-03-22 22:11:45
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 200 ms / 4,000 ms
コード長 1,420 bytes
コンパイル時間 1,757 ms
コンパイル使用メモリ 181,464 KB
実行使用メモリ 16,340 KB
最終ジャッジ日時 2023-08-15 11:59:55
合計ジャッジ時間 5,391 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 104 ms
10,568 KB
testcase_12 AC 97 ms
11,012 KB
testcase_13 AC 143 ms
13,088 KB
testcase_14 AC 56 ms
7,888 KB
testcase_15 AC 46 ms
7,096 KB
testcase_16 AC 163 ms
14,208 KB
testcase_17 AC 196 ms
16,064 KB
testcase_18 AC 194 ms
15,860 KB
testcase_19 AC 191 ms
16,340 KB
testcase_20 AC 101 ms
12,076 KB
testcase_21 AC 104 ms
12,172 KB
testcase_22 AC 38 ms
6,928 KB
testcase_23 AC 29 ms
5,976 KB
testcase_24 AC 87 ms
13,732 KB
testcase_25 AC 200 ms
15,984 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;

struct Edge { int to, cost; };

template<class T, class Edge> pair< V<T>, V<T> > dijkstra(const VV<Edge>& g, int s = 0) {
  V<T> dist(g.size(), numeric_limits<T>::max());
  using P = pair<T, int>;
  priority_queue< P, V<P>, greater<P> > pque;
  pque.emplace(dist[s] = 0, s);
  while (!pque.empty()) {
    T d; int v;
    tie(d, v) = pque.top(); pque.pop();
    if (d > dist[v]) continue;
    for (const auto& e : g[v]) if (dist[v] + e.cost < dist[e.to]) {
      pque.emplace(dist[e.to] = dist[v] + e.cost, e.to);
    } 
  }
  auto dp = dist;
  decltype(pque)().swap(pque);
  pque.emplace(dp[s] = 0, s);
  while (!pque.empty()) {
    T d; int v;
    tie(d, v) = pque.top(); pque.pop();
    if (d > dp[v]) continue;
    for (const auto& e : g[v]) {
      auto x = min(dist[v], dp[v] + e.cost);
      if (x < dp[e.to]) {
        pque.emplace(dp[e.to] = x, e.to);
      }
    }
  }
  return {dist, dp};
}

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  int n, m; cin >> n >> m;
  VV<Edge> g(n);
  while (m--) {
    int a, b, c; cin >> a >> b >> c, --a, --b;
    g[a].emplace_back(Edge{b, c});
    g[b].emplace_back(Edge{a, c});
  }
  auto p = dijkstra<lint>(g);
  for (int i = 0; i < n; ++i) {
    cout << p.first[i] + p.second[i] << '\n';
  }
}
0