QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#875641 | #9569. Subway | Andyqian7 | RE | 0ms | 0kb | C++26 | 2.5kb | 2025-01-30 00:21:59 | 2025-01-30 00:22:01 |
Judging History
answer
#include <bits/stdc++.h>
#define pii pair<int, int>
#define int long long
#define rep(i, s, e) for (int i = s; i <= e; i++)
using namespace std;
const int N = 4e5 + 10;
int n, k, a[N], b[N], p[N], d[N], belong[N], line[N], t, stp[N], vis[N], dis[N];
pii nxt[N];
vector<int> w[N], v[N];
deque<int> hull[N];
inline double slope(int x, int y)
{
if (b[line[x]] == b[line[y]])
return 1e18 * (dis[x] > dis[y]);
if (dis[x] == dis[y])
return 1e18 * (b[line[x]] > b[line[y]]);
return -1.0 * (b[line[x]] - b[line[y]]) / (dis[x] - dis[y]);
}
int cal(int x, int y)
{
return dis[x] + a[line[y]] * b[line[x]];
}
signed main()
{
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> k;
memset(d, 0x7f, sizeof d);
rep(i, 1, k) cin >> a[i];
rep(i, 1, k) cin >> b[i];
rep(i, 1, k)
{
cin >> p[i];
int w, x;
rep(j, 1, p[i] - 1)
{
cin >> x >> w;
v[x].push_back(++t);
belong[t] = x;
line[t] = i;
nxt[t] = {t + 1, w};
}
cin >> x;
v[x].push_back(++t);
belong[t] = x;
line[t] = i;
}
for (int i = 1; i <= n; i++)
{
sort(v[i].begin(), v[i].end(), [](int x, int y)
{ return a[line[x]] < a[line[y]]; });
}
priority_queue<pii, vector<pii>, greater<pii>> Q;
for (int x : v[1])
{
Q.push({0, x});
}
while (Q.size())
{
int x = Q.top().second;
int tmp = Q.top().first;
Q.pop();
if (vis[x])
continue;
vis[x] = 1;
dis[x] = tmp;
d[belong[x]] = min(d[belong[x]], dis[x]);
Q.pop();
int station = belong[x];
auto &hul = hull[station];
while (hul.size() >= 2 && slope(hul[hul.size() - 2], hul.back() < slope(hul.back(), x)))
hul.pop_back();
hul.push_back(x);
while (stp[station] < v[station].size() && vis[v[station][stp[station]]])
stp[station]++;
if (stp[station] < v[station].size())
{
int y = v[station][stp[station]];
while (hul.size() >= 2 && cal(hul[0], y) > cal(hul[1], y))
hul.pop_front();
Q.push({cal(hul[0], y), y});
}
if (nxt[x].first)
{
Q.push({dis[x] + nxt[x].second, nxt[x].first});
}
}
for (int i = 2; i <= n; i++)
{
cout << d[i] << " ";
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
6 3 1 5 1 5 5 1 3 1 2 2 3 3 3 5 1 2 1 4 3 3 4 5 4 6