結果

問題 No.160 最短経路のうち辞書順最小
ユーザー kazuto0215kazuto0215
提出日時 2016-07-20 15:14:24
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 1,232 bytes
コンパイル時間 1,451 ms
コンパイル使用メモリ 166,616 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-23 17:03:10
合計ジャッジ時間 2,473 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define F first
#define S second

using namespace std;

typedef struct {
  int to, cost;
} Edge;

typedef pair <int, int> P;
priority_queue <P> que;

int n, m, a, b, st, go, c;
long long node[222];

vector <Edge> G[222];

int main()
{
  cin >> n >> m >> st >> go;

  // グラフ入力
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;

    G[a].push_back((Edge){b, c});
    G[b].push_back((Edge){a, c});
  }

  // node初期化
  for (int i = 0; i < n; i++) {
    node[i] = 1 << 20;
  }

  // ダイク
  node[go] = 0;
  que.push(P(go, 0));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.F;
    
    if (node[v] < p.S) {
      continue;
    }

    for (int i = 0; i < G[v].size(); i++) {
      Edge e = G[v][i];

      if (node[e.to] > node[v] + e.cost) {
	node[e.to] = node[v] + e.cost;
	que.push(P(e.to, node[e.to]));
      }
    }
  }

  // 経路復元
  int now = st, next;

  while (now != go) {
    next = 1 << 20;
    cout << now << ' ';

    for (int i = 0; i < G[now].size(); i++) {
      Edge e = G[now][i];
      
      if (node[e.to] + e.cost == node[now]) {
	if (e.to < next) {
	  next = e.to;
	}
      }
    }
    now = next;
  }

  cout << go << endl;
}
0