結果

問題 No.160 最短経路のうち辞書順最小
ユーザー utahtautahta
提出日時 2020-02-02 23:41:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 15 ms / 5,000 ms
コード長 1,761 bytes
コンパイル時間 1,889 ms
コンパイル使用メモリ 179,696 KB
実行使用メモリ 4,440 KB
最終ジャッジ日時 2023-10-19 01:34:27
合計ジャッジ時間 3,114 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 5 ms
4,348 KB
testcase_05 AC 8 ms
4,348 KB
testcase_06 AC 11 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 4 ms
4,348 KB
testcase_09 AC 4 ms
4,348 KB
testcase_10 AC 4 ms
4,348 KB
testcase_11 AC 4 ms
4,348 KB
testcase_12 AC 4 ms
4,348 KB
testcase_13 AC 3 ms
4,348 KB
testcase_14 AC 3 ms
4,348 KB
testcase_15 AC 3 ms
4,348 KB
testcase_16 AC 4 ms
4,348 KB
testcase_17 AC 4 ms
4,348 KB
testcase_18 AC 3 ms
4,348 KB
testcase_19 AC 3 ms
4,348 KB
testcase_20 AC 4 ms
4,348 KB
testcase_21 AC 3 ms
4,348 KB
testcase_22 AC 4 ms
4,348 KB
testcase_23 AC 4 ms
4,348 KB
testcase_24 AC 4 ms
4,348 KB
testcase_25 AC 4 ms
4,348 KB
testcase_26 AC 3 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 15 ms
4,440 KB
testcase_29 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define each(i, c) for (auto& i : c)

typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1e9+7;

template<typename P, typename Q> ostream& operator << (ostream& os, pair<P, Q> p) { os << "(" << p.first << ": " << p.second << ")"; return os; }
template<typename T> ostream& operator << (ostream& os, vector<T> v) { os << "("; each (i, v) os << i << ", "; os << ")"; return os; }
template<typename K, typename V> ostream& operator << (ostream& os, map<K, V> m) { os << "{"; each (i, m) os << i << ", "; os << "}"; return os; }

int main() {
  ll n, m, s, g;
  cin >> n >> m >> s >> g;

  vector<vector<ll>> graph(n, vector<ll>());
  vector<vector<ll>> cost(n, vector<ll>(n, 0));
  vector<ll> dest(n, 1e18);

  for (ll i = 0; i < m; ++i) {
    ll a, b, c;
    cin >> a >> b >> c;

    graph[a].push_back(b);
    graph[b].push_back(a);
    cost[a][b] = c;
    cost[b][a] = c;
  }

  swap(s, g);
  priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> q;
  dest[s] = 0;
  q.push(pair<ll, ll>(0, s));

  while (!q.empty()) {
    auto p = q.top();
    q.pop();

    auto c = p.first;
    auto i = p.second;
    if (dest[i] < c) continue;

    each (j, graph[i]) {
      auto nc = cost[i][j] + c;
      if (nc >= dest[j]) continue;
      dest[j] = nc;
      q.push(pair<ll, ll>(nc, j));
    }
  }

  ll cur = g;
  vector<ll> ans = {cur};
  while (cur != s) {
    ll next = 1e18;
    each (i, graph[cur]) {
      if (dest[cur] == dest[i] + cost[cur][i]) {
        next = min(next, i);
      }
    }
    cur = next;
    ans.push_back(cur);
  }

  for (ll i = 0; i < ans.size(); ++i) {
    cout << ans[i];
    if (i == ans.size()-1) cout << endl;
    else cout << " ";
  }

  return 0;
}
0