結果

問題 No.160 最短経路のうち辞書順最小
ユーザー yuppe19 😺yuppe19 😺
提出日時 2015-06-26 16:57:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 7 ms / 5,000 ms
コード長 1,605 bytes
コンパイル時間 695 ms
コンパイル使用メモリ 77,852 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-22 00:46:52
合計ジャッジ時間 2,600 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;

class range {private: struct I{int x;int operator*(){return x;}bool operator!=(I& lhs){return x<lhs.x;}void operator++(){++x;}};I i,n;
public:range(int n):i({0}),n({n}){}range(int i,int n):i({i}),n({n}){}I& begin(){return i;}I& end(){return n;}};

struct edge {
  int to, cost;
  edge(int _to, int _cost) : to(_to), cost(_cost) {};
};

const int inf = 987654321;

int main(void) {
  int n, m, s, g; scanf("%d%d%d%d", &n, &m, &s, &g);
  vector<vector<edge>> G(n, vector<edge>());
  for(int i : range(m)) {
    int a, b, c; scanf("%d%d%d", &a, &b, &c);
    G[a].push_back(edge(b, c));
    G[b].push_back(edge(a, c));
  }
  vector<int> cost(n, inf);
  cost[g] = 0;
  priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> que;
  // first: コスト, second: 点番号
  que.push(make_pair(0, g));
  while(!que.empty()) {
    pair<int, int> cur = que.top(); que.pop();
    int v = cur.second;
    for(int i : range(G[v].size())) {
      edge e = G[v][i];
      if(cost[e.to] > cost[v] + e.cost) {
        cost[e.to] = cost[v] + e.cost;
        que.push(make_pair(cost[e.to], e.to));
      }
    }
  }

  int v = s;
  vector<int> res;
  res.push_back(v);
  while(v != g) {
    int next = inf;
    for(int i : range(G[v].size())) {
      edge e = G[v][i];
      if(cost[v] == cost[e.to] + e.cost) {
        next = min(next, e.to);
      }
    }
    v = next;
    res.push_back(v);
  }
  for(int i : range(res.size())) {
    if(i!=0) { putchar(' '); }
    printf("%d", res[i]);
  }
  puts("");
  return 0;
}
0