QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#300435#6306. Chase Gamemendicillin2#RE 1ms3552kbC++171.9kb2024-01-08 11:29:102024-01-08 11:29:10

Judging History

你现在查看的是最新测评结果

  • [2024-01-08 11:29:10]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3552kb
  • [2024-01-08 11:29:10]
  • 提交

answer

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

template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

using ll = int64_t;

int main() {
	ios_base::sync_with_stdio(false), cin.tie(nullptr);
	cout << fixed << setprecision(20);

	int N, M, K, D;
	cin >> N >> M >> K >> D;
	K--;

	VV<int> adj(N);
	for (int e = 0; e < M; e++) {
		int a, b;
		cin >> a >> b;
		a--, b--;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}

	auto get_dist = [&](int st) -> V<int> {
		V<int> q = {st};
		q.reserve(N);
		V<int> dist(N, -1);
		dist[st] = 0;
		for (int z = 0; z < int(q.size()); z++) {
			int cur = q[z];
			for (int nxt : adj[cur]) {
				if (dist[nxt] == -1) {
					dist[nxt] = dist[cur] + 1;
					q.push_back(nxt);
				}
			}
		}
		assert(int(q.size()) == N);
		return dist;
	};

	auto dist_N1 = get_dist(N-1);
	auto dist_K = get_dist(K);

	const ll INF = 1e18;
	V<ll> dist2(N, INF);
	struct Data {
		ll key;
		int i;

		bool operator < (const Data& o) const { return key > o.key; }
	};
	priority_queue<Data> pq;
	auto do_push = [&](int v, ll d) -> void {
		if (d < dist2[v]) {
			dist2[v] = d;
			pq.push(Data{d, v});
		}
	};
	do_push(0, 0);
	while (!pq.empty()) {
		auto [d, cur] = pq.top();
		pq.pop();
		assert(dist2[cur] <= d);
		if (dist2[cur] < d) continue;
		for (int nxt : adj[cur]) {
			if (dist_K[nxt] >= D) continue;
			ll nd = d + (D - dist_K[nxt]);
			do_push(nxt, nd);
		}
	}
	assert(dist2[0] == 0);

	ll ans = INF;
	auto eval = [&](ll d_n1) -> ll {
		d_n1++;
		ll nfull = d_n1 / D;
		ll nrest = d_n1 % D;
		return nfull * D * (D+1) / 2 + nrest * (D + (D-nrest+1)) / 2;
	};
	for (int f = 0; f < N-1; f++) {
		if (dist2[f] == INF) continue;
		for (int g : adj[f]) {
			if (dist_K[g] < D) continue;
			ll cost = dist2[f] + eval(dist_N1[g]);
			//cerr << "(" << f << ", " << g << "): cost = " << cost << endl;
			ans = min(ans, cost);
		}
	}
	assert(ans < INF);
	cout << ans << '\n';

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3440kb

input:

5 5 3 1
1 2
2 4
4 5
1 3
3 5

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3448kb

input:

13 17 12 3
1 2
2 3
3 4
4 13
5 13
7 8
7 9
7 10
7 11
7 6
12 7
1 8
8 9
9 10
10 11
11 6
6 13

output:

7

result:

ok 1 number(s): "7"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3496kb

input:

10 9 4 1
4 3
1 2
7 6
6 5
8 9
9 10
5 4
8 7
3 2

output:

9

result:

ok 1 number(s): "9"

Test #4:

score: 0
Accepted
time: 0ms
memory: 3496kb

input:

10 20 9 1
9 5
2 9
4 5
10 5
7 3
1 8
7 1
7 5
3 4
3 1
7 8
3 8
9 3
10 6
9 1
1 6
8 5
6 2
1 10
2 1

output:

1

result:

ok 1 number(s): "1"

Test #5:

score: 0
Accepted
time: 0ms
memory: 3436kb

input:

10 20 3 1
1 4
6 7
5 4
5 3
2 1
8 1
7 8
2 6
2 4
4 8
9 5
1 10
7 4
5 8
4 10
2 5
6 10
4 6
3 7
1 3

output:

1

result:

ok 1 number(s): "1"

Test #6:

score: 0
Accepted
time: 0ms
memory: 3552kb

input:

10 20 4 1
2 7
6 4
2 3
2 4
6 5
8 5
6 7
10 2
3 10
1 8
3 9
3 7
1 7
5 1
6 1
3 4
2 1
5 2
9 2
9 10

output:

2

result:

ok 1 number(s): "2"

Test #7:

score: -100
Runtime Error

input:

10 20 1 10
10 9
2 5
7 8
7 10
10 8
8 9
5 6
3 1
6 4
7 9
2 3
3 6
2 1
8 6
5 8
7 4
4 3
2 4
4 1
9 6

output:


result: