QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#753678 | #9569. Subway | ucup-team2112 | WA | 0ms | 3668kb | C++20 | 4.6kb | 2024-11-16 13:26:04 | 2024-11-16 13:26:09 |
Judging History
answer
#include <bits/stdc++.h>
template<class T>
struct Line {
mutable T k, m;
mutable double p;
bool operator<(const Line& o) const { return k > o.k; }
bool operator<(double x) const { return p < x; }
};
template<class T>
struct LineContainer : std::multiset<Line<T>, std::less<>> {
const double inf = 1.0 / .0;
double div(double a, double b) { return a / b; }
bool isect(typename std::multiset<Line<T>, std::less<>>::iterator x, typename std::multiset<Line<T>, std::less<>>::iterator y) {
if (y == this->end()) { x->p = inf; return false; }
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(T k, T m) {
auto z = this->insert({k, m, 0}), y = z++, x = y;
while (isect(y, z)) z = this->erase(z);
if (x != this->begin() && isect(--x, y)) isect(x, y = this->erase(y));
while ((y = x) != this->begin() && (--x)->p <= y->p)
isect(x, this->erase(y));
}
T query(T x) {
if (this->empty()) return std::numeric_limits<T>::max();
auto l = *this->lower_bound(x);
return l.k * x + l.m;
}
};
using i64 = long long;
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int n, m;
std::cin >> n >> m;
std::vector<LineContainer<i64> > lc(n);
std::vector<i64> a(m), b(m);
for (auto &x : a) std::cin >> x;
for (auto &x : b) std::cin >> x;
// st_ls[i][j] -> station i, line index j, which line and id
std::vector<std::vector<std::pair<int, int> > > st_ls(n);
std::vector<std::vector<int> > ln_ls(m);
std::vector<std::pair<int, int> > id_back;
int id = 0;
auto get_id = [&](int x, int y) {
auto it = std::lower_bound(st_ls[x].begin(), st_ls[x].end(), std::make_pair(y, -1));
assert(it->first == y);
return it->second;
};
for (int i = 0; i < m; i += 1) {
auto &ls = ln_ls[i];
int k;
std::cin >> k;
ls.resize(2 * k - 1);
for (int &x : ls) std::cin >> x;
for (int j = 0; j < ls.size(); j += 2) {
ls[j] -= 1;
id_back.emplace_back(ls[j], i);
st_ls[ls[j]].emplace_back(i, id++);
}
}
std::vector adj(id, std::vector<std::pair<int, i64> > ());
for (int i = 0; i < m; i += 1) {
auto &ls = ln_ls[i];
for (int j = 0; j + 1 < ls.size(); j += 2) {
int x = get_id(ls[j], i);
int y = get_id(ls[j + 2], i);
adj[x].emplace_back(y, ls[j + 1]);
}
}
for (int i = 0; i < n; i += 1) {
std::sort(st_ls[i].begin(), st_ls[i].end(), [&](auto x, auto y) {
return a[x.first] > a[y.first];
});
}
std::vector<i64> dp(id, 1e18);
std::priority_queue<std::pair<i64, int> > pq;
std::vector<int> vis(id);
for (auto [x, id] : st_ls[0]) {
dp[id] = 0;
pq.emplace(0, id);
}
std::vector<int> cnt(n);
auto pop = [&](int x) {
while(not st_ls[x].empty()) {
auto [y, id] = st_ls[x].back();
if (vis[id]) {
st_ls[x].pop_back();
continue;
}
i64 dis = lc[x].query(a[y]);
if (dp[id] > dis) {
dp[id] = dis;
// std::println("update2 ({}, {}) x = {}, y = {}, {}\n", id_back[id].first + 1, id_back[id].second + 1, x + 1, y + 1, dp[id]);
pq.emplace(-dp[id], id);
}
break;
}
};
while(not pq.empty()) {
auto [dis, u] = pq.top();
pq.pop();
dis = -dis;
if (vis[u]) continue;
vis[u] = 1;
auto [x, y] = id_back[u];
for (auto [v, w] : adj[u]) {
if (dp[v] > dp[u] + w) {
// std::println("update1 ({}, {}) from ({}, {}) with edge ({}, {})", id_back[v].first + 1, id_back[v].second + 1, id_back[u].first + 1, id_back[u].second + 1, dp[u], w);
dp[v] = dp[u] + w;
pq.emplace(-dp[v], v);
}
}
// std::println("add station {} line {} {}", x + 1, b[y], dp[u]);
lc[x].add(b[y], dp[u]);
pop(x);
}
std::vector<i64> res(n, 1e18);
for (int i = 0; i < id; i += 1) {
auto [x, y] = id_back[i];
res[x] = std::min(res[x], dp[i]);
}
for (int i = 1; i < n; i += 1) {
std::cout << res[i] << " \n"[i + 1 == n];
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3512kb
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
output:
2 5 21 14 18
result:
ok 5 number(s): "2 5 21 14 18"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3668kb
input:
6 3 1 5 1 5 5 1 5 1 2 2 100 3 100 6 1 4 5 1 100 2 4 3 100 5 1 4 2 3 1 5
output:
2 31 43 37 136
result:
ok 5 number(s): "2 31 43 37 136"
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 3508kb
input:
5 9 278281 70119 511791 834898 25300 63487 609134 236836 394497 835557 287345 579404 879128 636344 306393 569430 152565 47119 2 3 823004250 4 2 1 25427550 3 2 1 15849621 3 2 1 701911826 5 3 5 679672631 3 907176714 2 2 1 817370554 2 2 3 697987914 2 2 4 873900795 2 2 1 814305954 5
output:
817370554 15849621 192580219735 701911826
result:
wrong answer 3rd numbers differ - expected: '80811085745', found: '192580219735'