結果

問題 No.807 umg tours
ユーザー kk
提出日時 2021-02-03 22:48:06
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 355 ms / 4,000 ms
コード長 1,216 bytes
コンパイル時間 2,947 ms
コンパイル使用メモリ 220,172 KB
実行使用メモリ 21,860 KB
最終ジャッジ日時 2023-09-12 18:21:03
合計ジャッジ時間 10,569 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 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 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 155 ms
11,676 KB
testcase_12 AC 226 ms
13,632 KB
testcase_13 AC 284 ms
15,476 KB
testcase_14 AC 115 ms
9,008 KB
testcase_15 AC 91 ms
7,812 KB
testcase_16 AC 283 ms
15,732 KB
testcase_17 AC 355 ms
20,784 KB
testcase_18 AC 341 ms
21,680 KB
testcase_19 AC 328 ms
18,676 KB
testcase_20 AC 229 ms
14,472 KB
testcase_21 AC 240 ms
14,960 KB
testcase_22 AC 96 ms
8,268 KB
testcase_23 AC 76 ms
7,144 KB
testcase_24 AC 221 ms
19,504 KB
testcase_25 AC 352 ms
21,860 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

const long long INF = 1LL<<60;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n, m;
  cin >> n >> m;

  vector<vector<tuple<int, int> > > edges(n);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    --a, --b;
    edges[a].emplace_back(b, c);
    edges[b].emplace_back(a, c);
  }

  vector<vector<long long> > dist(n, vector<long long>(2, INF));
  
  priority_queue<tuple<long long, int, int>, vector<tuple<long long , int, int>>, greater<tuple<long long, int, int>> > Q;
  for (int x = 0; x < 2; x++) {
    dist[0][x] = 0;
    Q.emplace(dist[0][x], 0, x);
  }

  while (!Q.empty()) {
    long long cost;
    int v;
    int x;
    tie(cost, v, x) = Q.top();
    
    Q.pop();

    if (dist[v][x] < cost) continue;

    for (auto &[w, weight]: edges[v]) {
      if (dist[v][x] + weight < dist[w][x]) {
        dist[w][x] = dist[v][x] + weight;
        Q.emplace(dist[w][x], w, x);
      }
      if (!x && dist[v][x] < dist[w][x+1]) {
        dist[w][x+1] = dist[v][x];
        Q.emplace(dist[w][x+1], w, x+1);
      }
    }
  }
  
  for (int i = 0; i < n; i++) 
    cout << dist[i][0] + dist[i][1] << endl;
  
  return 0;
}
0