結果

問題 No.160 最短経路のうち辞書順最小
ユーザー purple_jwlpurple_jwl
提出日時 2015-03-03 00:09:07
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 13 ms / 5,000 ms
コード長 1,771 bytes
コンパイル時間 1,466 ms
コンパイル使用メモリ 153,900 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-06 06:33:21
合計ジャッジ時間 2,722 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

#define REP(i, x, n) for(int i = x; i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define F first
#define S second
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

const int INF = 1 << 30;

struct State {
  int pos, dist;
  State(int p, int d): pos(p), dist(d) {}
  bool operator < (const State &stt) const {
    return stt.dist < dist;
  }
};

struct Edge {
  int to, dist;
  Edge(int t, int d): to(t), dist(d) {}
};

int N, M, S, G;
vector<Edge> edge[200];
int prevv[200];
int minDist[200];

void solve() {
  rep(i, N) {
    prevv[i] = INF;
    minDist[i] = INF;
  }

  priority_queue<State> pq;
  pq.push(State(G, 0));
  minDist[G] = 0;
  prevv[G] = -1;
  
  while(!pq.empty()) {
    State stt = pq.top();
    pq.pop();

    rep(i, edge[stt.pos].size()) {
      int to = edge[stt.pos][i].to;
      int dist = edge[stt.pos][i].dist;
      if(minDist[to] > minDist[stt.pos] + dist) {
        prevv[to] = stt.pos;
        minDist[to] = minDist[stt.pos] + dist;
        pq.push(State(to, minDist[to]));
      }
      else if(minDist[to] == minDist[stt.pos] + dist) {
        prevv[to] = min(prevv[to], stt.pos);
      }
    }
  }

  vector<int> ans;
  ans.push_back(S);
  for(int p = prevv[S]; p != G; p = prevv[p]) {
    ans.push_back(p);
  }
  ans.push_back(G);

  rep(i, ans.size()) {
    if(i) cout << ' ';
    cout << ans[i];
  }
  cout << endl;
}

int main() {
  // ios_base::sync_with_stdio(false);
  cin >> N >> M >> S >> G;
  rep(i, M) {
    int a, b, c;
    cin >> a >> b >> c;
    edge[a].push_back(Edge(b, c));
    edge[b].push_back(Edge(a, c));
  }
  solve();
  return 0;
}
0