結果

問題 No.614 壊れたキャンパス
ユーザー FF256grhyFF256grhy
提出日時 2017-12-14 03:29:22
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 549 ms / 2,000 ms
コード長 2,871 bytes
コンパイル時間 3,415 ms
コンパイル使用メモリ 186,120 KB
実行使用メモリ 58,480 KB
最終ジャッジ日時 2023-08-21 03:01:04
合計ジャッジ時間 8,934 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
17,716 KB
testcase_01 AC 5 ms
17,764 KB
testcase_02 AC 6 ms
13,808 KB
testcase_03 AC 5 ms
13,532 KB
testcase_04 AC 6 ms
17,792 KB
testcase_05 AC 6 ms
17,820 KB
testcase_06 AC 6 ms
17,636 KB
testcase_07 AC 5 ms
13,656 KB
testcase_08 AC 436 ms
56,984 KB
testcase_09 AC 477 ms
56,208 KB
testcase_10 AC 400 ms
53,688 KB
testcase_11 AC 481 ms
57,952 KB
testcase_12 AC 549 ms
56,972 KB
testcase_13 AC 512 ms
58,480 KB
testcase_14 AC 463 ms
56,372 KB
testcase_15 AC 330 ms
55,240 KB
testcase_16 AC 400 ms
55,712 KB
testcase_17 AC 276 ms
46,772 KB
testcase_18 AC 298 ms
58,352 KB
testcase_19 AC 249 ms
55,072 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<tuple<LL, LL, LL>> 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.EB(a[i] + 0, b[i], 2 * i + 0);
		e.EB(a[i] + 1, c[i], 2 * i + 1);
	}
	e.EB(    0, s, 2 * m + 0);
	e.EB(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(get<0>(e[i + 0]) == get<0>(e[i + 1])) {
			v[get<2>(e[i + 0])].EB(get<2>(e[i + 1]), abs(get<1>(e[i + 0]) - get<1>(e[i + 1])));
			v[get<2>(e[i + 1])].EB(get<2>(e[i + 0]), abs(get<1>(e[i + 0]) - get<1>(e[i + 1])));
		}
	}
	
	dijkstra(2 * m + 0, v, d);
	
	cout << (d[2 * m + 1] < INF ? d[2 * m + 1] : -1) << endl;
	
	return 0;
}
0