結果

問題 No.160 最短経路のうち辞書順最小
ユーザー togatogatogatoga
提出日時 2015-04-04 16:38:04
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 12 ms / 5,000 ms
コード長 1,810 bytes
コンパイル時間 1,291 ms
コンパイル使用メモリ 153,772 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-17 04:25:38
合計ジャッジ時間 2,237 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

#define mp make_pair
#define mt make_tuple
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long, long> pll;

const int INF = 1 << 29;
const double EPS = 1e-9;
const int MOD = 100000007;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

struct edge{
  int b,c;
  edge(int _b, int _c){
    b = _b;
    c = _c;
  }
  bool operator < (const edge& e)const {
    return this->c < e.c;
  }
};
struct state{
  int pos, cost;
  state(int _pos, int _cost){
    pos = _pos;
    cost = _cost;
  }
  bool operator < (const state& s) const {
    return this->cost < s.cost;
  }
};

vector<edge> edges[220];
int d[220];
int N,M,S,G;
int path[220];
void dijkstra(int s){
  fill(path , path + N, INF);
  fill(d, d + N, INF);
  d[s] = 0;
  priority_queue<state> que;
  que.push(state(s, 0));
  while (!que.empty()){
    state p = que.top();
    que.pop();
    int v = p.pos;
    if (d[v] < p.cost)continue;
    for (int i = 0; i < edges[v].size(); i++){
      edge e = edges[v][i];
      if (d[e.b] > d[v] + e.c){
        d[e.b] = d[v] + e.c;
        path[e.b] = v;
        que.push(state(e.b, d[e.b]));
      } else if(d[e.b] == d[v] + e.c){
        path[e.b] = min(path[e.b], v);
      }
    }
  }
}
int main() {
  cin >> N >> M >> S >> G;
  for (int i = 0; i < M; i++){
    int a,b,c;
    cin >> a >> b >> c;
    edges[a].push_back(edge(b, c));
    edges[b].push_back(edge(a, c));
  }

  dijkstra(G);
  vector<int > res;
  int  p = S;
  while (p != G){
    res.push_back(p);
    p = path[p];
  }
  res.push_back(G);
//  reverse(res.begin(), res.end());
  for (int i = 0; i < res.size(); i++){
    cout << res[i] << " ";
  }
  cout << endl;
  return 0;
}
0