結果

問題 No.160 最短経路のうち辞書順最小
ユーザー simansiman
提出日時 2021-12-23 20:00:12
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 20 ms / 5,000 ms
コード長 1,616 bytes
コンパイル時間 1,485 ms
コンパイル使用メモリ 134,812 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-10-17 21:28:46
合計ジャッジ時間 2,568 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 6 ms
4,348 KB
testcase_05 AC 10 ms
4,348 KB
testcase_06 AC 13 ms
4,368 KB
testcase_07 AC 4 ms
4,348 KB
testcase_08 AC 4 ms
4,348 KB
testcase_09 AC 3 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 3 ms
4,348 KB
testcase_17 AC 3 ms
4,348 KB
testcase_18 AC 3 ms
4,348 KB
testcase_19 AC 3 ms
4,348 KB
testcase_20 AC 3 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 3 ms
4,348 KB
testcase_25 AC 3 ms
4,348 KB
testcase_26 AC 3 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 20 ms
4,504 KB
testcase_29 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

struct Node {
  int v;
  int parent;
  ll cost;

  Node(int v = -1, int parent = -1, ll cost = -1) {
    this->v = v;
    this->parent = parent;
    this->cost = cost;
  }

  bool operator>(const Node &n) const {
    return cost > n.cost;
  }
};

struct Edge {
  int u;
  ll cost;

  Edge(int u, ll cost) {
    this->u = u;
    this->cost = cost;
  }
};

vector<Edge> E[210];

int main() {
  int N, M, S, G;
  cin >> N >> M >> S >> G;

  for (int i = 0; i < M; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    E[a].push_back(Edge(b, c));
    E[b].push_back(Edge(a, c));
  }

  vector<ll> dist(N + 1, LLONG_MAX);
  priority_queue <Node, vector<Node>, greater<Node>> pque;
  pque.push(Node(G, -1, 0));

  while (not pque.empty()) {
    Node node = pque.top();
    pque.pop();

    if (dist[node.v] <= node.cost) continue;
    dist[node.v] = node.cost;

    for (auto e : E[node.v]) {
      ll ncost = node.cost + e.cost;
      pque.push(Node(e.u, node.v, ncost));
    }
  }

  vector<int> ans;
  int cur = S;
  ans.push_back(cur);
  while (true) {
    int next = 1 << 29;
    for (Edge &e : E[cur]) {
      if (dist[cur] == dist[e.u] + e.cost) {
        next = min(next, e.u);
      }
    }
    cur = next;
    ans.push_back(cur);
    if (cur == G) break;
  }

  for (int v : ans) {
    cout << v;
    if (v != ans.back()) cout << " ";
  }
  cout << endl;

  return 0;
}
0