結果

問題 No.614 壊れたキャンパス
ユーザー hiroakiehiroakie
提出日時 2017-12-18 14:38:03
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,208 bytes
コンパイル時間 803 ms
コンパイル使用メモリ 71,400 KB
実行使用メモリ 16,576 KB
最終ジャッジ日時 2024-05-09 11:21:09
合計ジャッジ時間 7,499 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
13,568 KB
testcase_01 AC 5 ms
8,064 KB
testcase_02 AC 6 ms
8,192 KB
testcase_03 AC 6 ms
8,064 KB
testcase_04 WA -
testcase_05 AC 6 ms
8,192 KB
testcase_06 WA -
testcase_07 AC 6 ms
8,192 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 292 ms
11,136 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 213 ms
11,008 KB
testcase_16 WA -
testcase_17 WA -
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++) {
		dp[(i + 1) % 2].reserve(G[i + 1].size());
		for (int j = 0; j < dp[(i + 1) % 2].size(); j++)dp[(i + 1) % 2][j] = -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