結果
問題 | No.160 最短経路のうち辞書順最小 |
ユーザー | koyumeishi |
提出日時 | 2015-03-02 00:05:34 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 63 ms / 5,000 ms |
コード長 | 2,102 bytes |
コンパイル時間 | 1,110 ms |
コンパイル使用メモリ | 93,308 KB |
実行使用メモリ | 8,016 KB |
最終ジャッジ日時 | 2024-06-24 00:45:23 |
合計ジャッジ時間 | 2,522 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 2 ms
6,944 KB |
testcase_04 | AC | 6 ms
6,944 KB |
testcase_05 | AC | 8 ms
6,944 KB |
testcase_06 | AC | 12 ms
6,940 KB |
testcase_07 | AC | 4 ms
6,944 KB |
testcase_08 | AC | 5 ms
6,940 KB |
testcase_09 | AC | 3 ms
6,944 KB |
testcase_10 | AC | 4 ms
6,944 KB |
testcase_11 | AC | 5 ms
6,948 KB |
testcase_12 | AC | 4 ms
6,944 KB |
testcase_13 | AC | 4 ms
6,940 KB |
testcase_14 | AC | 5 ms
6,944 KB |
testcase_15 | AC | 4 ms
6,940 KB |
testcase_16 | AC | 3 ms
6,944 KB |
testcase_17 | AC | 4 ms
6,944 KB |
testcase_18 | AC | 4 ms
6,944 KB |
testcase_19 | AC | 4 ms
6,944 KB |
testcase_20 | AC | 4 ms
6,940 KB |
testcase_21 | AC | 4 ms
6,940 KB |
testcase_22 | AC | 4 ms
6,940 KB |
testcase_23 | AC | 3 ms
6,940 KB |
testcase_24 | AC | 5 ms
6,944 KB |
testcase_25 | AC | 4 ms
6,944 KB |
testcase_26 | AC | 3 ms
6,940 KB |
testcase_27 | AC | 2 ms
6,940 KB |
testcase_28 | AC | 63 ms
8,016 KB |
testcase_29 | AC | 3 ms
6,940 KB |
ソースコード
#include <iostream> #include <vector> #include <cstdio> #include <sstream> #include <map> #include <string> #include <algorithm> #include <queue> #include <cmath> #include <set> using namespace std; typedef struct{ int to; int cost; }edge; class my_distance{ public: long long cost; vector<int> s; my_distance(){ } my_distance(long long cost_, const vector<int> s_){ cost = cost_; s = s_; } my_distance(const my_distance& x){ this->cost = x.cost; this->s = x.s; } bool operator<(const my_distance& x) const{ if(this->cost != x.cost) return this->cost < x.cost; return this->s < x.s; } bool operator>(const my_distance& x) const{ if(this->cost != x.cost) return this->cost > x.cost; return this->s > x.s; } }; //distance from s //O(E log V) void dijkstra(vector< vector<edge> > &G, vector< my_distance > &dist, int s){ //INF as distance const long long INF = 1LL<<60LL; //first : distance from s, second : its vertex priority_queue< pair<my_distance, int> , vector< pair<my_distance, int> >, greater< pair<my_distance, int> >> pq; fill(dist.begin(), dist.end(), my_distance(INF, {}) ); dist[s] = my_distance(0, {s}); pq.push( {dist[s], s} ); while(!pq.empty()){ auto q = pq.top(); pq.pop(); int from = q.second; if(dist[from] < q.first) continue; // it's not minimum distance int n=G[from].size(); for(int i=0; i<n; i++){ edge e = G[from][i]; my_distance next = dist[from]; next.cost += e.cost; next.s.push_back(e.to); if(dist[e.to] > next){ dist[e.to] = next; pq.push( {dist[e.to], e.to} ); } } } } void add_edge(vector<vector<edge> > &G, int from, int to, int cost){ G[from].push_back( (edge){to, cost} ); G[to].push_back( (edge){from, cost} ); } int main(){ int N,M,S,G; cin >> N >> M >> S >> G; vector<vector<edge>> Graph(N); for(int i=0; i<M; i++){ int a,b,c; cin >> a >> b >> c; add_edge(Graph, a, b, c); } vector<my_distance> dist(N); dijkstra(Graph, dist, S); auto ans = dist[G].s; for(int i=0; i<ans.size(); i++){ cout << ans[i] << " "; } cout << endl; return 0; }