QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#384245#5540. City HallFOYWA 196ms14540kbC++233.0kb2024-04-09 21:11:082024-04-09 21:11:08

Judging History

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

  • [2024-04-09 21:11:08]
  • 评测
  • 测评结果:WA
  • 用时:196ms
  • 内存:14540kb
  • [2024-04-09 21:11:08]
  • 提交

answer

#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <climits>
#include <cassert>
using namespace std;
using ll = long long;
const int mn = 1e5+5;
using pll = pair<ll, ll>;
using minpq = priority_queue<pll, vector<pll>, greater<pll>>;

struct Line {
	mutable ll k, m, p;
	bool operator<(const Line& o) const { return k < o.k; }
	bool operator<(ll x) const { return p < x; }
};

struct LineContainer : multiset<Line, less<>> {
	// (for doubles, use inf = 1/.0, div(a,b) = a/b)
	static const ll inf = LLONG_MAX;
	ll div(ll a, ll b) { // floored division
		return a / b - ((a ^ b) < 0 && a % b); }
	bool isect(iterator x, iterator y) {
		if (y == end()) return x->p = inf, 0;
		if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
		else x->p = div(y->m - x->m, x->k - y->k);
		return x->p >= y->p;
	}
	void add(ll k, ll m) {
		auto z = insert({k, m, 0}), y = z++, x = y;
		while (isect(y, z)) z = erase(z);
		if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
		while ((y = x) != begin() && (--x)->p >= y->p)
			isect(x, erase(y));
	}
	ll query(ll x) {
		assert(!empty());
		auto l = *lower_bound(x);
		return l.k * x + l.m;
	}
};

ll cost(vector<pll> x, vector<pll> y) {
	ll best = 1e18;
	LineContainer l;
	for (int i = 0; i < x.size(); i++) {
		auto [y1, x1] = x[i];
		l.add(-(-2*y1), -(y1*y1 + 2*x1));
	}
	for (int j = 0; j < y.size(); j++) {
		auto [y2, x2] = y[j];
		best = min(best, -l.query(y2) + 2*x2 + y2*y2);
	}
	return best;
}

ll h[mn];
vector<int> adj[mn];
ll from[mn], to[mn];
bool vis[mn];
int main() {
	ll n, m, s, t; cin >> n >> m >> s >> t;
	for (int i = 1; i <= n; i++) {
		cin >> h[i];
		from[i] = 5e18;
		to[i] = 5e18;
	}
	for (int i = 0; i < m; i++) {
		int u, v; cin >> u >> v;
		adj[v].push_back(u);
		adj[u].push_back(v);
	}
	minpq q;
	q.push({0, s});
	from[s] = 0;
	while (q.size()) {
		auto [d, v] = q.top();
		q.pop();
		if (vis[v]) continue;
		vis[v] = true;
		for (auto x : adj[v]) {
			if (vis[x]) continue;
			ll y = d + (h[x]-h[v])*(h[x]-h[v]);
			if (from[x] > y) {
				from[x] = y;
				q.push({y, x});
			}
		}
	}

	for (int i = 1; i <= n; i++) {
		vis[i] = false;
	}
	q.push({0, t});
	to[t] = 0;
	while (q.size()) {
		auto [d, v] = q.top();
		q.pop();
		if (vis[v]) continue;
		vis[v] = true;
		for (auto x : adj[v]) {
			if (vis[x]) continue;
			ll y = d + (h[x]-h[v])*(h[x]-h[v]);
			if (to[x] > y) {
				to[x] = y;
				q.push({y, x});
			}
		}
	}

	ll ans = from[t];
	assert(from[t] == to[s]);
	for (int i : adj[s]) {
		ans = min(ans, to[i]);
	}
	for (int i : adj[t]) {
		ans = min(ans, from[i]);
	}
	for (int i = 1; i <= n; i++) {
		assert(from[i] + to[i] >= from[t]);
	}
	for (int i = 1; i <= n; i++) {
		vector<pll> x, y;
		for (int j : adj[i]) {
			x.push_back({h[j], from[j]});
			y.push_back({h[j], to[j]});
		}
		ans = min(ans, cost(x, y));
	}
	cout << ans/2;
	if (ans%2) cout << ".5";
	else cout << ".0";
	cout << endl;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 5612kb

input:

5 6 1 3
5 100 8 2 10
1 2
2 3
2 5
1 4
4 5
3 5

output:

4.5

result:

ok found '4.5000000', expected '4.5000000', error '0.0000000'

Test #2:

score: 0
Accepted
time: 1ms
memory: 7644kb

input:

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

output:

3.0

result:

ok found '3.0000000', expected '3.0000000', error '0.0000000'

Test #3:

score: 0
Accepted
time: 1ms
memory: 7708kb

input:

5 4 1 4
8 8 8 8 100
1 2
2 3
3 4
4 5

output:

0.0

result:

ok found '0.0000000', expected '0.0000000', error '-0.0000000'

Test #4:

score: 0
Accepted
time: 1ms
memory: 3568kb

input:

2 1 1 2
0 100000
1 2

output:

0.0

result:

ok found '0.0000000', expected '0.0000000', error '-0.0000000'

Test #5:

score: 0
Accepted
time: 150ms
memory: 14540kb

input:

632 199396 167 549
22513 93521 41403 35441 97617 53210 62622 4751 81863 14470 2994 93092 40432 30872 34423 36577 92540 92961 4110 14691 83321 10680 89112 80890 31108 24492 8973 42636 33792 27400 82361 85003 68940 31221 48625 276 52755 6649 34381 54399 6063 22628 17715 54052 58175 86609 82622 29917 9...

output:

0.0

result:

ok found '0.0000000', expected '0.0000000', error '-0.0000000'

Test #6:

score: 0
Accepted
time: 129ms
memory: 8020kb

input:

600 179700 396 574
83219 94660 9266 1939 32637 7117 33396 76947 42614 41001 87026 60210 25868 36044 35276 6147 36198 25195 50981 68230 32619 62563 28509 46643 43304 36195 99724 30903 77230 57923 36939 81397 17374 90947 48623 38120 48914 96481 98146 31707 9427 58735 82986 31328 94507 69081 51908 4188...

output:

0.0

result:

ok found '0.0000000', expected '0.0000000', error '-0.0000000'

Test #7:

score: -100
Wrong Answer
time: 196ms
memory: 13152kb

input:

100000 200000 66364 98254
48399 8344 35365 18555 95271 13113 75220 25062 73969 71647 58212 68205 36310 45496 6965 59960 71592 81323 44341 95796 61521 63298 77830 16145 73103 83477 85246 53680 67289 68475 64978 43576 18969 28023 22848 55164 31276 12825 70999 49142 77203 40134 88148 59337 2357 70519 8...

output:

868746117.0

result:

wrong answer 1st numbers differ - expected: '1019365473.0000000', found: '868746117.0000000', error = '0.1477580'