結果

問題 No.160 最短経路のうち辞書順最小
ユーザー utahtautahta
提出日時 2020-02-02 23:26:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,823 bytes
コンパイル時間 1,922 ms
コンパイル使用メモリ 180,076 KB
実行使用メモリ 813,148 KB
最終ジャッジ日時 2023-10-19 01:33:20
合計ジャッジ時間 9,203 ms
ジャッジサーバーID
(参考情報)
judge14 / 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 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 MLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

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;
  }

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

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

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

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

  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);
  }
  reverse(ans.begin(), ans.end());

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

  return 0;
}
0