結果

問題 No.614 壊れたキャンパス
ユーザー ふっぴーふっぴー
提出日時 2017-12-15 01:29:53
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,049 ms / 2,000 ms
コード長 2,694 bytes
コンパイル時間 2,016 ms
コンパイル使用メモリ 191,720 KB
実行使用メモリ 95,844 KB
最終ジャッジ日時 2023-08-21 05:09:23
合計ジャッジ時間 12,506 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
34,064 KB
testcase_01 AC 15 ms
33,984 KB
testcase_02 AC 15 ms
33,896 KB
testcase_03 AC 16 ms
34,108 KB
testcase_04 AC 16 ms
33,904 KB
testcase_05 AC 16 ms
34,008 KB
testcase_06 AC 16 ms
34,052 KB
testcase_07 AC 16 ms
33,900 KB
testcase_08 AC 789 ms
94,528 KB
testcase_09 AC 1,049 ms
93,876 KB
testcase_10 AC 653 ms
95,844 KB
testcase_11 AC 888 ms
95,356 KB
testcase_12 AC 949 ms
95,312 KB
testcase_13 AC 920 ms
95,624 KB
testcase_14 AC 835 ms
95,632 KB
testcase_15 AC 595 ms
94,304 KB
testcase_16 AC 701 ms
94,320 KB
testcase_17 AC 363 ms
87,376 KB
testcase_18 AC 516 ms
81,848 KB
testcase_19 AC 500 ms
83,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define DEBUG(x) cout<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cout<<#v<<":";for(int i=0;i<v.size();i++) cout<<" "<<v[i]; cout<<endl

typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
const int inf = 1000000001;
const ll INF = 1e18 * 2;
#define MOD 1000000007
#define mod 1000000009
#define pi 3.14159265358979323846
#define Sp(p) cout<<setprecision(15)<< fixed<<p<<endl;
int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };


static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
static const int N = 400020;

vector< vector<pll> > adj(N, vector<pll>());
vl d(N, INF);
map<ll, ll> mp[N];
int cnt = 1;


void dijkstra() {
	priority_queue<pll, vector<pll>, greater<pll> > pq;
	int i;
	vi color(cnt, WHITE);

	d[1] = 0;
	pq.push(make_pair(0, 1));
	color[1] = GRAY;

	while (!pq.empty()) {
		pll f = pq.top();
		pq.pop();
		ll u = f.second;
		color[u] = BLACK;
		if (d[u]<f.first) {
			continue;
		}
		for (i = 0; i < adj[u].size(); i++) {
			ll v = adj[u][i].second;
			if (color[v] == BLACK) {
				continue;
			}
			if (d[v]>d[u] + adj[u][i].first) {
				d[v] = d[u] + adj[u][i].first;
				pq.push(make_pair(d[v], v));
				color[v] = GRAY;
			}
		}
	}
}


int main() {
	int n, m, k, s, t, i;
	cin >> n >> m >> k >> s >> t;
	s--; t--;
	vll floor(n);
	floor[0].push_back(s);
	floor[n - 1].push_back(t);
	mp[0][s] = cnt++;
	mp[n-1][t] = cnt++;
	for (i = 0; i < m; i++) {
		ll a, b, c;
		cin >> a >> b >> c;
		a--; b--; c--;
		if (mp[a][b] == 0) {
			mp[a][b] = cnt++;
			floor[a].push_back(b);
		}
		if (mp[a + 1][c] == 0) {
			mp[a + 1][c] = cnt++;
			floor[a + 1].push_back(c);
		}
		adj[mp[a][b]].push_back(pll(0, mp[a + 1][c]));
	}
	for (i = 0; i < n; i++) {
		sort(floor[i].begin(), floor[i].end());
		for (int j = 0; j + 1 < floor[i].size();j++) {
			int a = floor[i][j];
			int b = floor[i][j + 1];
			if (a != b) {
				//cout << i << " " << a << " " << b << endl;
				ll u = mp[i][a], v = mp[i][b];
				//cout << i << " " << u << " " << v << endl;
				adj[u].push_back(pll(abs(b - a), v));
				adj[v].push_back(pll(abs(a - b), u));
			}
		}
	}
	/*
	for (i = 0; i < cnt; i++) {
		for (int j = 0; j < adj[i].size(); j++) {
			printf("(%d, %d, %d)  ", i, adj[i][j].first, adj[i][j].second);
		}
		cout << endl;
	}
	*/
	dijkstra();
	if (d[mp[n - 1][t]] == INF) {
		cout << -1 << endl;
	}
	else {
		cout << d[mp[n - 1][t]] << endl;
	}
}
0