結果

問題 No.160 最短経路のうち辞書順最小
ユーザー kazuto0215kazuto0215
提出日時 2016-07-20 15:27:42
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 15 ms / 5,000 ms
コード長 1,245 bytes
コンパイル時間 1,362 ms
コンパイル使用メモリ 167,696 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-23 17:03:54
合計ジャッジ時間 2,292 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 5 ms
6,940 KB
testcase_05 AC 7 ms
6,944 KB
testcase_06 AC 10 ms
6,940 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 4 ms
6,944 KB
testcase_12 AC 3 ms
6,944 KB
testcase_13 AC 3 ms
6,940 KB
testcase_14 AC 3 ms
6,940 KB
testcase_15 AC 3 ms
6,940 KB
testcase_16 AC 3 ms
6,944 KB
testcase_17 AC 3 ms
6,940 KB
testcase_18 AC 3 ms
6,940 KB
testcase_19 AC 4 ms
6,944 KB
testcase_20 AC 4 ms
6,944 KB
testcase_21 AC 3 ms
6,940 KB
testcase_22 AC 3 ms
6,940 KB
testcase_23 AC 3 ms
6,940 KB
testcase_24 AC 4 ms
6,940 KB
testcase_25 AC 3 ms
6,940 KB
testcase_26 AC 3 ms
6,940 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 15 ms
6,944 KB
testcase_29 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:26:27: warning: narrowing conversion of ‘b’ from ‘long long int’ to ‘int’ [-Wnarrowing]
   26 |     G[a].push_back((Edge){b, c});
      |                           ^
main.cpp:26:30: warning: narrowing conversion of ‘c’ from ‘long long int’ to ‘int’ [-Wnarrowing]
   26 |     G[a].push_back((Edge){b, c});
      |                              ^
main.cpp:27:27: warning: narrowing conversion of ‘a’ from ‘long long int’ to ‘int’ [-Wnarrowing]
   27 |     G[b].push_back((Edge){a, c});
      |                           ^
main.cpp:27:30: warning: narrowing conversion of ‘c’ from ‘long long int’ to ‘int’ [-Wnarrowing]
   27 |     G[b].push_back((Edge){a, c});
      |                              ^

ソースコード

diff #

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

using namespace std;

typedef struct {
  int to, cost;
} Edge;

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

long long n, m, a, b, st, go, c, 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 << 30;
  }

  // ダイク
  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]));
      }
    }
  }

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

  while (now != go) {
    next = 1 << 30;
    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