QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#113824 | #5546. Sharing Bread | PetroTarnavskyi# | RE | 0ms | 0kb | C++17 | 2.4kb | 2023-06-19 16:40:07 | 2023-06-19 16:40:09 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define FOR(i, a, b) for (int i = (a); i<(b); ++i)
#define RFOR(i, b, a) for (int i = (b)-1; i>=(a); --i)
#define MP make_pair
#define PB push_back
#define F first
#define S second
typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
const LL LINF = 1e18;
const int N = 1 << 17;
LL h[N];
LL dist[2][N];
vector<int> g[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, s, t;
cin >> n >> m >> s >> t;
s--;
t--;
FOR(i, 0, n) {
cin >> h[i];
}
FOR(i, 0, m) {
int u, v;
cin >> u >> v;
u--;
v--;
g[u].push_back(v);
g[v].push_back(u);
}
FOR(it, 0, 2) {
FOR(i, 0, N) {
dist[it][i] = LINF;
}
priority_queue<pair<LL, int>> pq;
int v0 = it == 0 ? s : t;
dist[it][v0] = 0;
pq.emplace(0, v0);
while (!pq.empty()) {
auto [d, v] = pq.top();
pq.pop();
d *= -1;
if (d != dist[it][v]) {
continue;
}
for (int to : g[v]) {
LL nd = d + (h[to] - h[v]) * (h[to] - h[v]);
if (nd < dist[it][to]) {
dist[it][to] = nd;
pq.emplace(-nd, to);
}
}
}
}
LL ans = 2 * dist[0][t];
FOR(v, 0, n) {
vector<pair<LL, LL>> vec, convex;
for (int j : g[v]) {
vec.emplace_back(-2 * h[j], 2 * dist[1][j] + h[j] * h[j]);
}
sort(ALL(vec), [](pair<LL, LL> p1, pair<LL, LL> p2) {
if (p1.first != p2.first) {
return p1.first > p2.first;
}
return p1.second > p2.second;
});
for (auto [k, b] : vec) {
if (SZ(convex) > 1) {
auto [k1, b1] = convex[SZ(convex) - 2];
auto [k2, b2] = convex.back();
if (k == k2 || (k - k2) * (b2 - b1) <= (b2 - b) * (k1 - k2)) {
convex.pop_back();
}
}
convex.emplace_back(k, b);
}
for (int i : g[v]) {
int l = 0, r = SZ(convex) - 1;
while (r - l > 2) {
int m1 = (l + r) / 2, m2 = m1 + 1;
auto [k1, b1] = convex[m1];
auto [k2, b2] = convex[m2];
if (k1 * h[i] + b1 < k2 * h[i] + b2) {
r = m2;
}
else {
l = m1;
}
}
LL cur = LINF;
for (; l <= r; l++) {
auto [k, b] = convex[l];
cur = min(cur, k * h[i] + b);
}
ans = min(ans, 2 * dist[0][i] + h[i] * h[i] + cur);
}
}
cout << ans / 2;
if (ans & 1) {
cout << ".5";
}
cout << "\n";
return 0;
}
詳細信息
Test #1:
score: 0
Runtime Error
input:
4 3