QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#90971 | #5458. Shortest Path Query | Indus | WA | 51ms | 28860kb | C++14 | 2.0kb | 2023-03-26 11:10:46 | 2024-06-21 12:37:52 |
Judging History
answer
#include <bits/stdc++.h>
#define eb emplace_back
#define fi first
#define se second
#define pii pair<int, int>
#define mp make_pair
using namespace std;
template<typename Tp>
void read(Tp &x) {
x = 0; char ch = getchar(); int f = 0;
for(; !isdigit(ch); f |= (ch == '-'), ch = getchar());
for(; isdigit(ch); x = (x<<3)+(x<<1)+(ch^48), ch = getchar());
if (f) x = ~x + 1;
}
typedef long long LL;
const LL inf = 9e18;
const int N = 1e5 + 5;
int n, m, deg[N];
vector<pii> G[N], vec[N];
double slope(pii i, pii j) {
if (i.fi == j.fi) return inf;
return 1.0 * (i.se - j.se) / (i.fi - j.fi);
}
pii operator + (const pii &a, const pii &b) {return mp(a.fi + b.fi, a.se + b.se);}
void TopuSort() {
queue<int> Q; Q.push(1), vec[1].eb(mp(0, 0));
while (!Q.empty()) {
int x = Q.front(); Q.pop();
sort(vec[x].begin(), vec[x].end());
vector<pii> f;
for(pii k : vec[x]) {
while (f.size() > 1 && slope(f.back(), f[f.size() - 2]) >= slope(k, f.back())) f.pop_back();
f.eb(k);
}
swap(vec[x], f);
for(pii e : G[x]) {
for(pii k : vec[x]) {
vec[e.fi].eb(k + mp(!e.se, e.se)), --deg[e.fi];
if (!deg[e.fi]) Q.push(e.fi);
}
}
}
}
int calc(int a, int b, pii k){return a * k.fi + b * k.se;}
int Query(int u, int a, int b) {
if (vec[u].size() == 1) calc(a, b, vec[u][0]);
double k = -1.0 * a / b;
int l = 0, r = vec[u].size() - 2, mid = l + r >> 1, bst = 0;
for(; l <= r; mid = l + r >> 1)
if (slope(vec[u][mid], vec[u][mid + 1]) >= k) bst = mid, r = mid - 1; else l = mid + 1;
return min(calc(a, b, vec[u][bst]), calc(a, b, vec[u].back()));
}
int main() {
read(n), read(m);
for(int i = 1, u, v, w; i <= m; i++) read(u), read(v), read(w), G[u].eb(mp(v, w)), ++deg[v];
TopuSort(); int q; read(q);
for(int a, b, u; q; --q) read(a), read(b), read(u), printf("%d\n", Query(u, a, b));
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 8396kb
input:
4 4 1 2 0 1 3 1 2 4 0 3 4 1 3 3 5 2 3 2 4 2 3 4
output:
3 4 4
result:
ok 3 number(s): "3 4 4"
Test #2:
score: 0
Accepted
time: 39ms
memory: 17628kb
input:
50000 100000 1 2 1 2 3 0 3 4 1 4 5 0 5 6 1 6 7 0 7 8 1 8 9 1 9 10 0 10 11 1 11 12 1 12 13 1 13 14 0 14 15 0 15 16 0 16 17 0 17 18 1 18 19 1 19 20 0 20 21 1 21 22 0 22 23 0 23 24 1 24 25 1 25 26 0 26 27 1 27 28 0 28 29 0 29 30 0 30 31 0 31 32 1 32 33 0 33 34 1 34 35 1 35 36 1 36 37 1 37 38 0 38 39 0 ...
output:
164602050 208733870 228180204 248456409 87574800 16685198 46684062 64713954 46949896 240633535 94777502 83016099 259833741 167838804 214963500 147454419 111021650 80187604 184782450 78138570 86528820 203553394 188095596 202049239 290053220 172790198 168899028 97757186 96431009 266952297 164349486 26...
result:
ok 50000 numbers
Test #3:
score: -100
Wrong Answer
time: 51ms
memory: 28860kb
input:
50000 100000 1 2 1 2 3 0 3 4 1 4 5 0 5 6 0 6 7 1 7 8 0 8 9 0 9 10 1 10 11 1 11 12 1 12 13 1 13 14 0 14 15 1 15 16 1 16 17 1 17 18 0 18 19 0 19 20 1 20 21 1 21 22 1 22 23 0 23 24 0 24 25 1 25 26 1 26 27 0 27 28 0 28 29 1 29 30 0 30 31 1 31 32 1 32 33 1 33 34 0 34 35 1 35 36 1 36 37 1 37 38 0 38 39 0 ...
output:
100398788 31414635 97057314 4877973 131372151 101401294 97972523 116465260 89286626 138309577 111227766 96931397 98894394 113784862 103437724 35891862 119591018 27385428 145395927 53389420 44991380 178247166 102795173 124131241 70849452 29005113 62367612 55557236 83480745 101099140 24142023 82761051...
result:
wrong answer 1st numbers differ - expected: '100196045', found: '100398788'