結果

問題 No.1995 CHIKA Road
ユーザー MasKoaTS
提出日時 2022-02-19 17:10:23
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 674 ms / 2,000 ms
コード長 1,390 bytes
コンパイル時間 1,176 ms
コンパイル使用メモリ 99,728 KB
最終ジャッジ日時 2025-01-28 01:00:49
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <map>
#define rep(i, l, n) for (int i = (l); i < (n); i++)
using namespace std;
using Pair = pair<int, int>;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T> >;
const int inf = 2000000000;


int dijkstra(int N, int start, int goal, VV<Pair>&route) {
	V<int> path(N, inf);
	priority_queue<Pair, V<Pair>, greater<Pair> > pq;
	pq.push({ 0,start });
	while (pq.empty() == false) {
		Pair p = pq.top();	pq.pop();
		int d = p.first;	int v = p.second;
		if (path[v] <= d) {
			continue;
		}
		if (v == goal) {
			return d;
		}
		path[v] = d;
		for(auto& p : route[v]){
			pq.push({ d + p.second,p.first });
		}
	}
	return -1;
}


int main(void) {
	int n, m;	cin >> n >> m;
	set<int> st = { 1,n };
	VV<int> edge(m);
	rep(i, 0, m) {
		int a, b;	cin >> a >> b;
		st.insert(a);	st.insert(b);
		edge[i] = { a,b };
	}
	int N = st.size();

	int cnt = 0, last = -1;
	map<int, int> mp;
	VV<Pair> route(N, V<Pair>(0));
	for (auto itr = st.begin(); itr != st.end(); itr++) {
		mp[*itr] = cnt;
		cnt++;
		if (last > -1) {
			route[mp[last]].push_back({ mp[*itr],2 * (*itr) - 2 * last });
		}
		last = *itr;
	}
	for (auto& e : edge) {
		int a = mp[e[0]], b = mp[e[1]];
		route[a].push_back({ b,2 * e[1] - 2 * e[0] - 1 });
	}

	int ans = dijkstra(N, 0, N - 1, route);
	cout << ans << endl;

	return 0;
}
0