結果

問題 No.614 壊れたキャンパス
ユーザー olpheolphe
提出日時 2017-12-14 02:38:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,176 bytes
コンパイル時間 1,493 ms
コンパイル使用メモリ 126,068 KB
実行使用メモリ 62,600 KB
最終ジャッジ日時 2024-05-08 08:24:07
合計ジャッジ時間 7,997 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 WA -
testcase_05 AC 1 ms
5,376 KB
testcase_06 WA -
testcase_07 AC 2 ms
5,376 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 405 ms
62,540 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 399 ms
57,744 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "iomanip"
#include "cmath"

using namespace std;

const long long int MOD = 1000000007;

long long int N, M, K, H, W, L, R;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> N >> M >> K >> L >> R;
	vector<pair<long long int, long long int>>edge;
	vector<map<int, int>>m(N + 1);
	map<long long int, long long int>dis;
	for (int i = 0; i < M; i++) {
		long long int a, b, c;
		cin >> a >> b >> c;
		m[a][b]++;
		m[a + 1][c]++;
		edge.push_back({ a*MOD + b,(a + 1)*MOD + c });
		edge.push_back({ (a + 1)*MOD + c,a*MOD + b });
	}
	sort(edge.begin(), edge.end());
	m[1][L]++;
	m[N][R]++;
	for (int i = 1; i <= N; i++) {
		for (auto j : m[i]) {
			dis[i*MOD + j.first] = LLONG_MAX;
		}
	}
	dis[MOD + L] = 0;
	priority_queue<pair<long long int, long long int>, vector<pair<long long int, long long int>>, greater<pair<long long int, long long int>>>PQ;
	PQ.push({ 0,MOD + L });
	while (!PQ.empty()) {
		auto box = PQ.top();
		PQ.pop();
		int cw = box.second / MOD;
		int ch = box.second %MOD;
		int cc = box.first;
		if (dis[cw*MOD + ch] > cc) {
			continue;
		}
		auto it = m[cw].find(ch);
		if (it != m[cw].begin()) {
			it--;
			int nh = (*it).first;
			if (dis[cw*MOD + nh] > cc + abs(nh - ch)) {
				dis[cw*MOD + nh] = cc + abs(nh - ch);
				PQ.push({ dis[cw*MOD + nh],cw*MOD + nh });
			}
			it++;
		}
		it++;
		if (it != m[cw].end()) {
			int nh = (*it).first;
			if (dis[cw*MOD + nh] > cc + abs(nh - ch)) {
				dis[cw*MOD + nh] = cc + abs(nh - ch);
				PQ.push({ dis[cw*MOD + nh],cw*MOD + nh });
			}
		}
		auto itb = lower_bound(edge.begin(), edge.end(), make_pair(cw*MOD + ch, (long long int)0));
		auto ite = lower_bound(edge.begin(), edge.end(), make_pair(cw*MOD + ch + 1, (long long int)0));
		for (; itb != ite; ++itb) {
			int n = (*itb).second;
			if (dis[n] > cc) {
				dis[n] = cc;
				PQ.push({ cc,n });
			}
		}
	}
	if (dis[N*MOD + R] == LLONG_MAX) {
		cout << -1 << endl;
		return 0;
	}
	cout << dis[N*MOD + R] << endl;
	return 0;
}
0