結果

問題 No.614 壊れたキャンパス
ユーザー FF256grhyFF256grhy
提出日時 2017-12-14 03:32:52
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 551 ms / 2,000 ms
コード長 2,832 bytes
コンパイル時間 1,857 ms
コンパイル使用メモリ 182,796 KB
実行使用メモリ 58,824 KB
最終ジャッジ日時 2023-08-21 03:01:15
合計ジャッジ時間 8,243 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
17,732 KB
testcase_01 AC 6 ms
17,680 KB
testcase_02 AC 6 ms
13,560 KB
testcase_03 AC 6 ms
13,616 KB
testcase_04 AC 6 ms
17,676 KB
testcase_05 AC 6 ms
17,660 KB
testcase_06 AC 7 ms
17,672 KB
testcase_07 AC 5 ms
13,808 KB
testcase_08 AC 448 ms
56,180 KB
testcase_09 AC 495 ms
55,384 KB
testcase_10 AC 402 ms
54,700 KB
testcase_11 AC 494 ms
57,220 KB
testcase_12 AC 551 ms
57,844 KB
testcase_13 AC 500 ms
57,912 KB
testcase_14 AC 465 ms
56,372 KB
testcase_15 AC 336 ms
54,772 KB
testcase_16 AC 396 ms
55,316 KB
testcase_17 AC 283 ms
45,316 KB
testcase_18 AC 302 ms
58,824 KB
testcase_19 AC 255 ms
54,860 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long   signed int LL;
typedef long long unsigned int LU;

#define incID(i, l, r) for(int i = (l)    ; i <  (r); i++)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); i++)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); i--)
#define  inc(i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define  dec(i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)

#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))

#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define PQ priority_queue

#define  ALL(v)  v.begin(),  v.end()
#define RALL(v) v.rbegin(), v.rend()
#define  FOR(it, v) for(auto it =  v.begin(); it !=  v.end(); ++it)
#define RFOR(it, v) for(auto it = v.rbegin(); it != v.rend(); ++it)

template<typename T> bool   setmin(T & a, T b) { if(b <  a) { a = b; return true; } else { return false; } }
template<typename T> bool   setmax(T & a, T b) { if(b >  a) { a = b; return true; } else { return false; } }
template<typename T> bool setmineq(T & a, T b) { if(b <= a) { a = b; return true; } else { return false; } }
template<typename T> bool setmaxeq(T & a, T b) { if(b >= a) { a = b; return true; } else { return false; } }
template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

// ---- ----

template<typename T> class RPQ : public PQ<T, vector<T>, greater<T>> { };
template<typename Cost> void dijkstra(int start, vector<pair<int, Cost>> g[], Cost dist[]) {
	RPQ<pair<Cost, int>> q;
	dist[start] = 0;
	q.emplace(dist[start], start);
	while(! q.empty()) {
		auto cp = q.top(); q.pop();
		int  p = cp.SE;
		Cost c = cp.FI;
		if(dist[p] != c) { continue; }
		for(auto && e : g[p]) {
			int  ep = e.FI;
			Cost ec = e.SE;
			if(setmin(dist[ep], dist[p] + ec)) { q.emplace(dist[ep], ep); }
		}
	}
	return;
}

LL n, m, k, s, t, a[200000], b[200000], c[200000], d[400002], INF = 1e15;
vector<array<LL, 3>> e;
vector<pair<int, LL>> v[400002];

int main() {
	cin >> n >> m >> k >> s >> t;
	inc(i, m) { cin >> a[i] >> b[i] >> c[i]; a[i]--; }
	
	inc(i, m) {
		e.PB({ a[i] + 0, b[i], 2 * i + 0 });
		e.PB({ a[i] + 1, c[i], 2 * i + 1 });
	}
	e.PB({     0, s, 2 * m + 0 });
	e.PB({ n - 1, t, 2 * m + 1 });
	sort(ALL(e));
	
	inc(i, 2 * m + 2) { d[i] = INF; }
	
	inc(i, m) { v[2 * i + 0].EB(2 * i + 1, 0); }
	inc(i, e.size() - 1) {
		if(e[i + 0][0] == e[i + 1][0]) {
			v[e[i + 0][2]].EB(e[i + 1][2], abs(e[i + 0][1] - e[i + 1][1]));
			v[e[i + 1][2]].EB(e[i + 0][2], abs(e[i + 0][1] - e[i + 1][1]));
		}
	}
	
	dijkstra(2 * m + 0, v, d);
	
	cout << (d[2 * m + 1] < INF ? d[2 * m + 1] : -1) << endl;
	
	return 0;
}
0