結果

問題 No.160 最短経路のうち辞書順最小
ユーザー purple_jwlpurple_jwl
提出日時 2015-03-01 23:52:08
言語 C++11
(gcc 11.4.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,771 bytes
コンパイル時間 1,305 ms
コンパイル使用メモリ 154,624 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-06 06:02:32
合計ジャッジ時間 2,618 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 4 ms
4,376 KB
testcase_05 AC 7 ms
4,380 KB
testcase_06 AC 9 ms
4,376 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 3 ms
4,376 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 3 ms
4,380 KB
testcase_20 AC 3 ms
4,380 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 2 ms
4,380 KB
testcase_28 WA -
testcase_29 AC 2 ms
4,376 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 prevN[200];
int minDist[200];

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

  priority_queue<State> pq;
  pq.push(State(S, 0));
  minDist[S] = 0;

  while(!pq.empty()) {
    State stt = pq.top();
    pq.pop();

    if(stt.pos == G) break;

    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 ||
         (minDist[to] == minDist[stt.pos] + dist && prevN[to] > stt.pos)) {
        prevN[to] = stt.pos;
        minDist[to] = minDist[stt.pos] + dist;
        pq.push(State(to, minDist[to]));
      }
    }
  }

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

  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