QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#277768 | #5540. City Hall | NYCU_template# | WA | 2ms | 6028kb | C++14 | 2.8kb | 2023-12-06 22:35:59 | 2023-12-06 22:36:00 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const int N = 100005;
vector<int> adj[N];
ll h[N];
int n, m;
vector<ll> Dijkstra(int src) {
vector<ll> dis(n, 1e18);
vector<ll> vis(n, false);
using pll = pair<ll, int>;
priority_queue<pll, vector<pll>, greater<pll>> pq;
dis[src] = 0;
pq.push({0, src});
while(!pq.empty()) {
int x = pq.top().second;
pq.pop();
if(vis[x]) continue;
for(auto v : adj[x]) {
ll w = (h[v] - h[x]) * (h[v] - h[x]);
if(dis[v] > dis[x] + w) {
dis[v] = dis[x] + w;
pq.push({dis[v], v});
}
}
}
return dis;
}
const ll INF = 1e10;
struct Line {
ll a, b; // ax + b
double val(ll x) { return (a - x) * (a - x) / 2.0 + b; }
Line() {}
Line(ll _a, ll _b) : a(_a), b(_b) {}
};
struct Node {
int L, R;
Line line;
Node *lc, *rc;
};
int cur;
void build(Node *r, int L, int R) {
r->L = L, r->R = R;
r->line = Line(1e5, 1e18);
if(L == R) return;
int M = (L + R) / 2;
build(r->lc = new Node(), L, M);
build(r->rc = new Node(), M + 1, R);
}
void update(Node *r, Line line) {
Line left_line, right_line;
if(line.a > r->line.a) {
left_line = line;
right_line = r->line;
} else {
left_line = r->line;
right_line = line;
}
int M = (r->L + r->R) / 2;
ll Mx = h[adj[cur][M]];
if(left_line.val(Mx) < right_line.val(Mx)) {
r->line = left_line;
if(r->L != r->R) update(r->rc, right_line);
} else {
r->line = right_line;
if(r->L != r->R) update(r->lc, left_line);
}
}
double qry(Node *r, int x) {
if(r->L == r->R)
return r->line.val(x);
int M = (r->L + r->R) / 2;
ll Mx = h[adj[cur][M]];
if(x <= Mx) return min(r->line.val(x), qry(r->lc, x));
else return min(r->line.val(x), qry(r->rc, x));
}
int main() {
int s, t;
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 a, b;
cin >> a >> b;
a--, b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
vector<ll> from_src = Dijkstra(s), to_dst = Dijkstra(t);
double ans = INF;
for(int i = 0; i < n; i++) {
sort(adj[i].begin(), adj[i].end(), [&](int a, int b) {
return h[a] < h[b];
});
cur = i;
Node *root;
build(root = new Node(), 0, (int)adj[i].size());
for(auto v : adj[i]) {
ans = min(qry(root, h[v]) + to_dst[v], ans);
update(root, Line(h[v], from_src[v]));
}
}
cout << fixed << setprecision(10) << ans << "\n";
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5968kb
input:
5 6 1 3 5 100 8 2 10 1 2 2 3 2 5 1 4 4 5 3 5
output:
4.5000000000
result:
ok found '4.5000000', expected '4.5000000', error '0.0000000'
Test #2:
score: 0
Accepted
time: 1ms
memory: 6028kb
input:
5 5 1 5 1 2 10 10 4 1 2 2 3 2 4 3 5 4 5
output:
3.0000000000
result:
ok found '3.0000000', expected '3.0000000', error '0.0000000'
Test #3:
score: 0
Accepted
time: 1ms
memory: 5876kb
input:
5 4 1 4 8 8 8 8 100 1 2 2 3 3 4 4 5
output:
0.0000000000
result:
ok found '0.0000000', expected '0.0000000', error '-0.0000000'
Test #4:
score: -100
Wrong Answer
time: 2ms
memory: 5972kb
input:
2 1 1 2 0 100000 1 2
output:
10000000000.0000000000
result:
wrong answer 1st numbers differ - expected: '0.0000000', found: '10000000000.0000000', error = '10000000000.0000000'