QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#482890 | #5540. City Hall | karuna | WA | 1ms | 7984kb | C++20 | 3.2kb | 2024-07-18 00:09:12 | 2024-07-18 00:09:12 |
Judging History
answer
#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int SZ = 202020;
int n, m, S, T, h[SZ];
vector<int> g[SZ];
ll ds[2][SZ];
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin >> n >> m >> S >> T;
--S; --T;
for (int i = 0; i < n; i++) {
cin >> h[i];
}
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
g[u - 1].push_back(v - 1);
g[v - 1].push_back(u - 1);
}
if (n + m < 5000) {
ll ans = 9e18;
for (int b = 0; b < n; b++) {
for (int t = 0; t < 2; t++) {
priority_queue<pll> pq;
for (int i = 0; i < n; i++) {
ds[t][i] = 1e18;
}
if (t == 0 && b == S) continue;
if (t == 1 && b == T) continue;
pq.push({0, t == 0 ? S : T});
ds[t][t == 0 ? S : T] = 0;
while (!pq.empty()) {
auto [d, v] = pq.top();
pq.pop();
if (ds[t][v] != -d) continue;
for (int x : g[v]) if (b != x) {
ll w = 1ll * (h[v] - h[x]) * (h[v] - h[x]);
if (ds[t][x] > ds[t][v] + w) {
ds[t][x] = ds[t][v] + w;
pq.push({-ds[t][x], x});
}
}
}
}
for (int x : g[b]) for (int y : g[b]) if (x != y) {
if (ds[0][x] == 1e18 || ds[1][y] == 1e18) continue;
ans = min(ans, 2 * ds[0][x] + 1ll * (h[x] - h[y]) * (h[x] - h[y]) + 2 * ds[1][y]);
}
}
cout << ans / 2 << (ans % 2 == 1 ? ".5" : ".0") << '\n';
return 0;
}
for (int t = 0; t < 2; t++) {
priority_queue<pll> pq;
for (int i = 0; i < n; i++) {
ds[t][i] = 1e18;
}
pq.push({0, t == 0 ? S : T});
ds[t][t == 0 ? S : T] = 0;
while (!pq.empty()) {
auto [d, v] = pq.top();
pq.pop();
if (ds[t][v] != -d) continue;
for (int x : g[v]) {
ll w = 1ll * (h[v] - h[x]) * (h[v] - h[x]);
if (ds[t][x] > ds[t][v] + w) {
ds[t][x] = ds[t][v] + w;
pq.push({-ds[t][x], x});
}
}
}
}
ll ans = 9e18;
for (int v = 0; v < n; v++) {
vector<pll> cht;
int sz = g[v].size();
int ord[sz];
iota(ord, ord + sz, 0);
sort(ord, ord + sz, [&](int i, int j) {
return h[g[v][i]] < h[g[v][j]];
});
for (int i = 0; i < sz; i++) {
int x = g[v][ord[i]];
ll e = -2 * h[x];
ll f = 1ll * h[x] * h[x] + 2 * ds[1][x];
if (!cht.empty() && cht.back().ff == e) {
if (cht.back().ss < f) {
f = cht.back().ss;
}
cht.pop_back();
}
while (cht.size() >= 2) {
auto [a, b] = cht[cht.size() - 2];
auto [c, d] = cht.back();
if ((__int128)(c - e) * (d - b) >= (__int128)(a - c) * (f - d)) cht.pop_back();
else break;
}
cht.push_back({e, f});
}
int pos = 0;
for (int i = 0; i < sz; i++) {
int x = g[v][ord[i]];
while (pos < (int)cht.size() - 1) {
auto [a, b] = cht[pos];
auto [c, d] = cht[pos + 1];
if ((__int128)h[x] * (a - c) >= (d - b)) ++pos;
else break;
}
ans = min(ans, cht[pos].ff * h[x] + cht[pos].ss + 1ll * h[x] * h[x] + 2 * ds[0][x]);
}
}
for (int x : g[S]) ans = min(ans, 2 * ds[1][x]);
for (int x : g[T]) ans = min(ans, 2 * ds[0][x]);
cout << ans / 2 << (ans % 2 == 1 ? ".5" : ".0") << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5948kb
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: 3612kb
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: 5744kb
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: -100
Wrong Answer
time: 1ms
memory: 7984kb
input:
2 1 1 2 0 100000 1 2
output:
4500000000000000000.0
result:
wrong answer 1st numbers differ - expected: '0.0000000', found: '4500000000000000000.0000000', error = '4500000000000000000.0000000'