結果

問題 No.807 umg tours
ユーザー risujirohrisujiroh
提出日時 2019-03-22 22:01:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,595 bytes
コンパイル時間 2,369 ms
コンパイル使用メモリ 190,704 KB
実行使用メモリ 20,872 KB
最終ジャッジ日時 2023-10-19 09:10:50
合計ジャッジ時間 5,690 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 2 ms
4,348 KB
testcase_05 WA -
testcase_06 AC 2 ms
4,348 KB
testcase_07 WA -
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 171 ms
17,248 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 118 ms
16,596 KB
testcase_21 AC 127 ms
17,228 KB
testcase_22 AC 44 ms
9,332 KB
testcase_23 AC 33 ms
8,120 KB
testcase_24 AC 84 ms
20,500 KB
testcase_25 WA -
権限があれば一括ダウンロードができます

ソースコード

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());
  VV<> h(g.size());
  V<> dh(g.size());
  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]) {
      h[v].push_back(e.to);
      ++dh[e.to];
      pque.emplace(dist[e.to] = dist[v] + e.cost, e.to);
    } 
  }
  V<T> dp(g.size(), 1e18);
  dp[s] = 0;
  V<> vs;
  queue<int> que;
  for (int v = 0; v < g.size(); ++v) if (!dh[v]) {
    que.push(v);
  }
  while (!que.empty()) {
    int v = que.front(); que.pop();
    vs.push_back(v);
    for (int w : h[v]) {
      if (!--dh[w]) {
        que.push(w);
      }
    }
  }
  for (int v : vs) {
    for (const auto& e : g[v]) {
      dp[e.to] = min({dp[e.to], dp[v] + e.cost, dist[v], dist[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