結果

問題 No.614 壊れたキャンパス
ユーザー hiroakiehiroakie
提出日時 2017-12-18 14:52:53
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,161 bytes
コンパイル時間 633 ms
コンパイル使用メモリ 72,396 KB
実行使用メモリ 18,784 KB
最終ジャッジ日時 2024-05-09 11:21:28
合計ジャッジ時間 7,106 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
13,696 KB
testcase_01 AC 5 ms
8,124 KB
testcase_02 AC 4 ms
8,008 KB
testcase_03 AC 5 ms
8,124 KB
testcase_04 AC 5 ms
8,092 KB
testcase_05 AC 4 ms
8,144 KB
testcase_06 AC 5 ms
8,192 KB
testcase_07 AC 5 ms
8,008 KB
testcase_08 AC 172 ms
10,624 KB
testcase_09 AC 210 ms
12,628 KB
testcase_10 AC 266 ms
11,116 KB
testcase_11 AC 213 ms
10,304 KB
testcase_12 AC 272 ms
10,448 KB
testcase_13 AC 222 ms
10,496 KB
testcase_14 AC 200 ms
10,752 KB
testcase_15 AC 189 ms
10,720 KB
testcase_16 AC 183 ms
11,520 KB
testcase_17 AC 235 ms
14,336 KB
testcase_18 TLE -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
typedef pair<int, int> str;
typedef long long int ll;
const int N = 200000;
vector<str> G[N - 1];
vector<ll> dp[2];
int n, m, k, s, t;

ll mn(ll a, ll b) {
	if (a == -1)return b; if (b == -1)return a;
	return min(a, b);
}

int main() {
	cin >> n >> m >> k >> s >> t;
	for (int i = 0; i < m; i++) {
		int a, b, c; cin >> a >> b >> c;
		G[a - 1].push_back(make_pair(b, c));
	}
	for (int i = 0; i < G[0].size(); i++) { dp[0].push_back(abs(G[0][i].first - s)); }
	for (int i = 0; i < n - 2; i++) {
		for (int j = 0; j < G[i+1].size(); j++)dp[(i + 1) % 2].push_back(-1);
		for (int j = 0; j < G[i].size(); j++) {
			ll cp = G[i][j].second,cv=dp[i%2][j];
			for (int k = 0; k < G[i + 1].size(); k++) {
				dp[(i + 1) % 2][k] = mn(dp[(i + 1) % 2][k], cv + abs(cp - G[i + 1][k].first));
			}
		}
		dp[i % 2].clear();
	}

	if (n == 1) { cout << abs(t - s) << endl; }
	else {
		ll ans = -1;
		for (int j = 0; j < dp[(n - 2) % 2].size(); j++) {
			ans = mn(ans, dp[(n - 2) % 2][j] + abs(t - G[n - 2][j].second));
		}
		cout << ans << endl;
	}

	return 0;
}
0