結果

問題 No.614 壊れたキャンパス
ユーザー startcppstartcpp
提出日時 2017-12-29 21:28:52
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 904 ms / 2,000 ms
コード長 2,479 bytes
コンパイル時間 914 ms
コンパイル使用メモリ 85,464 KB
実行使用メモリ 82,096 KB
最終ジャッジ日時 2023-08-23 09:04:45
合計ジャッジ時間 11,197 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
26,908 KB
testcase_01 AC 13 ms
26,804 KB
testcase_02 AC 12 ms
27,052 KB
testcase_03 AC 12 ms
26,776 KB
testcase_04 AC 12 ms
27,112 KB
testcase_05 AC 13 ms
26,908 KB
testcase_06 AC 12 ms
26,864 KB
testcase_07 AC 12 ms
26,828 KB
testcase_08 AC 791 ms
76,004 KB
testcase_09 AC 759 ms
77,952 KB
testcase_10 AC 749 ms
75,120 KB
testcase_11 AC 843 ms
75,856 KB
testcase_12 AC 904 ms
75,884 KB
testcase_13 AC 827 ms
76,264 KB
testcase_14 AC 785 ms
76,152 KB
testcase_15 AC 655 ms
75,144 KB
testcase_16 AC 731 ms
75,380 KB
testcase_17 AC 403 ms
71,748 KB
testcase_18 AC 482 ms
82,096 KB
testcase_19 AC 357 ms
73,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <algorithm>
#include <queue>
#include <functional>
#define int long long
using namespace std;
typedef pair<int, int> P;

int INF = 1e+15;
int n, m, k, s, t;
int a[200000], b[200000], c[200000];
vector<P> points;
vector<int> heights[200001];
vector<int> edgeTo[400002];
vector<int> edgeCost[400002];


int dijkstra(int start, int goal) {
	static int minCost[400002];
	static priority_queue<P, vector<P>, greater<P>> que;
	int i;
	
	for (i = 0; i < points.size(); i++) minCost[i] = INF;
	
	que.push(P(0, start));
	while (!que.empty()) {
		P now = que.top(); que.pop();
		int cst = now.first;
		int pos = now.second;
		if (minCost[pos] <= cst) continue;
		minCost[pos] = cst;
		if (pos == goal) break;
		
		for (i = 0; i < edgeTo[pos].size(); i++) {
			que.push(P(edgeCost[pos][i] + cst, edgeTo[pos][i]));
		}
	}
	return minCost[goal];
}

signed main() {
	int i, j;
	
	cin >> n >> m >> k >> s >> t;
	for (i = 0; i < m; i++) {
		cin >> a[i] >> b[i] >> c[i];
		points.push_back(P(a[i], b[i]));
		points.push_back(P(a[i] + 1, c[i]));
		heights[a[i]].push_back(b[i]);
		heights[a[i] + 1].push_back(c[i]);
	}
	points.push_back(P(1, s));
	points.push_back(P(n, t));
	sort(points.begin(), points.end());
	points.erase(unique(points.begin(), points.end()), points.end());
	heights[1].push_back(s);
	heights[n].push_back(t);
	for (i = 1; i <= n; i++) sort(heights[i].begin(), heights[i].end());
	
	for (i = 0; i < m; i++) {
		int from = lower_bound(points.begin(), points.end(), P(a[i], b[i])) - points.begin();
		int to   = lower_bound(points.begin(), points.end(), P(a[i] + 1, c[i])) - points.begin();
		edgeTo[from].push_back(to);
		edgeCost[from].push_back(0);
	}
	for (i = 1; i <= n; i++) {
		for (j = 0; j + 1 < heights[i].size(); j++) {
			int from = lower_bound(points.begin(), points.end(), P(i, heights[i][j])) - points.begin();
			int to   = lower_bound(points.begin(), points.end(), P(i, heights[i][j + 1])) - points.begin();
			edgeTo[from].push_back(to);
			edgeTo[to].push_back(from);
			edgeCost[from].push_back(heights[i][j + 1] - heights[i][j]);
			edgeCost[to].push_back(heights[i][j + 1] - heights[i][j]);
		}
	}
	
	int start = lower_bound(points.begin(), points.end(), P(1, s)) - points.begin();
	int goal  = lower_bound(points.begin(), points.end(), P(n, t)) - points.begin();
	int minCost = dijkstra(start, goal);
	
	if (minCost >= INF) cout << -1 << endl;
	else cout << minCost << endl;
	return 0;
}
0