結果
| 問題 |
No.160 最短経路のうち辞書順最小
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-06-26 16:57:18 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 6 ms / 5,000 ms |
| コード長 | 1,605 bytes |
| コンパイル時間 | 807 ms |
| コンパイル使用メモリ | 75,512 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-07 17:41:40 |
| 合計ジャッジ時間 | 1,896 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 26 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:17:24: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
17 | int n, m, s, g; scanf("%d%d%d%d", &n, &m, &s, &g);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:20:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
20 | int a, b, c; scanf("%d%d%d", &a, &b, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
ソースコード
#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;
}