結果

問題 No.807 umg tours
ユーザー yataro800
提出日時 2023-01-19 21:32:08
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 624 ms / 4,000 ms
コード長 1,683 bytes
コンパイル時間 4,479 ms
コンパイル使用メモリ 240,492 KB
実行使用メモリ 28,548 KB
最終ジャッジ日時 2024-06-22 12:39:25
合計ジャッジ時間 12,814 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <math.h>

#include <algorithm>
#include <array>
#include <atcoder/all>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <vector>

using namespace std;
using namespace atcoder;

using Graph = vector<vector<int>>;

using ll = long long;
typedef pair<ll, ll> P_ll;
typedef pair<int, int> P;

const ll INF_ll = 1e17;
const int INF = 1e8;
template <class T>
bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<P_ll>> G(N);
  for (int i = 0; i < M; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    G[a].push_back({b, c});
    G[b].push_back({a, c});
  }
  vector<vector<ll>> dist(N, vector<ll>(2, INF_ll));
  min_priority_queue<pair<P_ll, ll>> pq;
  dist[0][0] = 0;
  pq.push({{0, 0}, 0});
  while (!pq.empty()) {
    auto p = pq.top();
    pq.pop();
    ll d = p.first.first;
    ll c = p.first.second;
    ll u = p.second;
    if (dist[u][c] < d) {
      continue;
    }

    for (auto e : G[u]) {
      ll next = e.first;
      if (chmin(dist[next][c], e.second + d)) {
        pq.push({{dist[next][c], c}, next});
      }
      if (c == 0) {
        if (chmin(dist[next][1], d)) {
          pq.push({{dist[next][1], 1}, next});
        }
      }
    }
  }
  for (int i = 0; i < N; i++) {
    cout << dist[i][0] + min(dist[i][0], dist[i][1]) << endl;
  }

  return 0;
}
0