結果

問題 No.160 最短経路のうち辞書順最小
ユーザー tkzw_21tkzw_21
提出日時 2015-03-02 13:27:21
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,679 bytes
コンパイル時間 2,826 ms
コンパイル使用メモリ 159,588 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-06 06:30:12
合計ジャッジ時間 3,377 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
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 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef pair<int,int> P;
typedef pair<int,pair<int,int>> PP;
typedef long long ll;

const double EPS = 1e-8;
const int INF = 1e9;
const int MOD = 1e9+7;

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

struct edge{
	int to,cost;
	bool operator<(const edge &e)const{return to < e.to;}
	bool operator==(const edge &e)const{return to == e.to;}
};

vector<int> dijkstra(int s,int g,vector<vector<edge>>&es){
	priority_queue<pair<int,int>,vector<P>,greater<P>> pq;
	vector<int> dist(es.size(),INF);
	
	pq.push(make_pair(0,s));
	dist[s] = 0;
	
	while(!pq.empty()){
		pair<int,int> p = pq.top();pq.pop();
		int u = p.second,d = p.first;
		for(int i=0;i<es[u].size();i++){
			int  v = es[u][i].to,c = es[u][i].cost;
			if(dist[v] == INF || dist[v] > dist[u] + c){
				dist[v] = d + c;
				pq.push(make_pair(d+c,v));
			}
		}
	}

	vector<int> path;
	path.push_back(g);
	int u = g;
	while(s != u){
		for(int i=0;i<es[u].size();i++){
			int v = es[u][i].to,c = es[u][i].cost;
			if(dist[u] - dist[v] == c){
				path.push_back(v);
				u = v;
				break;
			}
		}
	}
	return path;
}



int main(void) {
	int n,m,s,g;
	cin >> n >> m >> s >> g;
	vector<vector<edge>> es(n);

	for(int i=0;i<m;i++){
		int a,b,c;
		cin >> a >> b >> c;
		es[a].push_back(edge{b,c});
		es[b].push_back(edge{a,c});
	}
	for(int i=0;i<n;i++){
		sort(es[i].begin(),es[i].end());
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<es[i].size();j++){
			cout << es[i][j].to << " ";
		}
		cout << endl;
	}

	vector<int> path = dijkstra(g,s,es);
	for(int i=0;i<path.size();i++){
		cout << path[i];
		if(path.size()-1 != i)cout << " ";
	}
	cout << endl;

	return 0;
}

//入力、添字
0