結果

問題 No.859 路線A、路線B、路線C
ユーザー sprng_wlsprng_wl
提出日時 2019-08-09 22:51:29
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,442 bytes
コンパイル時間 1,377 ms
コンパイル使用メモリ 157,040 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-26 21:11:35
合計ジャッジ時間 2,283 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define Int int64_t

using namespace std;

template <typename T>
struct edge {
	int to;
	T cost;
	edge(int t, T c) : to(t), cost(c) {}
};
template <typename T>
void dijkstra(const vector<vector< edge<T>  >>& G, vector<T>& d, int s) {
	priority_queue<pair<T, int>, vector< pair<T, int> >, greater< pair<T, int> >> que;
	d[s] = 0;
	que.emplace(0, s);

	while (!que.empty()) {
		auto p = que.top();
		que.pop();
		int u = p.second;
		if (d[u] < p.first) { continue; }

		for (auto e : G[u]) {
			if (d[e.to] <= d[u] + e.cost) { continue; }
			d[e.to] = d[u] + e.cost;
			que.emplace(d[e.to], e.to);
		}
	}
}

int main() {
	const Int INF = 1e18;
	vector<vector< edge<Int> >> g(7);
	for (int i = 1; i <= 6; ++i) {
		for (int j = 1; j <= 6; ++j) {
			if (i == j || (i % 2 != j % 2)) { continue; }
			g[i].emplace_back(j, 1);
		}
	}

	Int x[3];
	for (int i = 0; i < 3; ++i) {
		cin >> x[i];
		int idx = i * 2 + 1;
		g[idx].emplace_back(idx + 1, x[i] - 1);
		g[idx + 1].emplace_back(idx, x[i] - 1);
	}
	char s0, s1;
	Int t0, t1;
	cin >> s0 >> t0;
	cin >> s1 >> t1;

	int idx = (int)(s0 - 'A') * 2 + 1;
	g[0].emplace_back(idx, t0 - 1);
	g[0].emplace_back(idx + 1, x[int(s0 - 'A')] - t0);
	vector<Int> d(7, INF);
	dijkstra(g, d, 0);

	idx = (int)(s1 - 'A') * 2 + 1;
	Int ans = min(d[idx] + (t1 - 1), d[idx + 1] + (x[int(s1 - 'A')] - t1));
	if (s0 == s1) {
		ans = min(ans, abs(t0 - t1));
	}
	cout << ans << endl;

	return 0;
}
0