結果

問題 No.160 最短経路のうち辞書順最小
ユーザー utahtautahta
提出日時 2020-04-10 22:49:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,198 bytes
コンパイル時間 2,947 ms
コンパイル使用メモリ 179,720 KB
実行使用メモリ 4,376 KB
最終ジャッジ日時 2023-10-14 03:34:52
合計ジャッジ時間 4,013 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

#define each(i, c) for (auto& i : c)
#define mkp(a, b) make_pair(a, b)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> Pll;
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; }

typedef struct edge {
  ll to;
  ll cost;
} edge;
typedef vector<vector<edge>> G; // graph
typedef vector<Pll> GR; // graph result (cost, vertex num)

GR dijkstra(G &g, ll s) { // O(ElogV)
  priority_queue<Pll, vector<Pll>, greater<Pll>> q;
  GR res(g.size(), mkp(1e18, 0));
  
  res[s] = mkp(0, s);
  q.push(res[s]);

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

    ll cost = p.first;
    ll pos = p.second;
    if (res[pos].first < cost) continue;

    each (j, g[pos]) {
      ll to = j.to;
      ll to_cost = j.cost + cost;
      if (to_cost >= res[to].first) continue;
      res[to].first = to_cost;
      q.push(mkp(to_cost, to));
    }
  }
  return res;
}

vector<ll> dijkstra_restore_path(G &g, GR &res, ll s, ll e) {
  ll from = e;
  vector<ll> path = {from};
  while (from != s) {
    ll to = 1e18;
    each (i, g[from]) {
      if (res[from].first == res[i.to].first + i.cost) {
        to = i.to;
        break;
      }
    }
    from = to;
    path.push_back(from);
  }
  //reverse(path.begin(), path.end());
  return path;
}

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

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

    graph[a].push_back(edge{.to = b, .cost = c});
    graph[b].push_back(edge{.to = a, .cost = c});
  }

  swap(s, g);
  auto res = dijkstra(graph, s);
  auto ans = dijkstra_restore_path(graph, res, s, g);

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

  return 0;
}
0