結果

問題 No.160 最短経路のうち辞書順最小
ユーザー simansiman
提出日時 2021-12-23 19:33:51
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 1,828 bytes
コンパイル時間 2,271 ms
コンパイル使用メモリ 134,832 KB
実行使用メモリ 814,576 KB
最終ジャッジ日時 2023-10-17 20:57:16
合計ジャッジ時間 6,263 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 5 ms
4,348 KB
testcase_05 AC 9 ms
4,348 KB
testcase_06 AC 14 ms
4,376 KB
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 <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));
  }

  priority_queue <Node, vector<Node>, greater<Node>> pque;
  pque.push(Node(S, -1, 0));
  ll visited[N + 1];
  memset(visited, -1, sizeof(visited));
  int step_history[N + 1];
  memset(step_history, -1, sizeof(step_history));

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

    if (visited[node.v] == -1) {
      visited[node.v] = node.cost;
      step_history[node.v] = node.parent;
    } else if (visited[node.v] < node.cost) {
      continue;
    } else {
      step_history[node.v] = min(step_history[node.v], node.parent);
    }

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

  vector<int> path;
  int cur = G;
  path.push_back(G);

  while (cur != S) {
    cur = step_history[cur];
    path.push_back(cur);
  }

  reverse(path.begin(), path.end());
  string ans = "";
  for (int p : path) {
    ans += to_string(p);
    if (p != path.back()) ans += " ";
  }
  cout << ans << endl;


  return 0;
}
0