QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#560107 | #8673. 最短路径 | forgotmyhandle | 0 | 4041ms | 543320kb | C++14 | 4.0kb | 2024-09-12 12:38:18 | 2024-09-12 12:38:19 |
Judging History
answer
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <random>
#include <tuple>
#include <queue>
#define int long long
using namespace std;
const int inf = 0x7ffffffffffffff;
vector<pair<int, int> > gs[200005];
vector<pair<int, int> > gt[200005];
int n, m, q, seed;
struct node {
int x, dis, cur;
};
bool operator<(node a, node b) { return a.dis > b.dis; }
int ds[200005], dt[200005];
int vs[200005], vt[200005];
vector<int> Ss, St;
priority_queue<node> qs, qt;
int Query(int s, int t) {
if (s == t)
return 0;
if (gs[s].empty() || gt[t].empty())
return -1;
int z = -1;
ds[s] = dt[t] = 0;
vs[s] = vt[t] = 1;
Ss.emplace_back(s);
St.emplace_back(t);
qs.push((node) { s, gs[s][0].second, 0 });
qt.push((node) { t, gt[t][0].second, 0 });
while (!qs.empty() && !qt.empty()) {
if (qs.size() <= qt.size()) {
node tmp = qs.top();
qs.pop();
int x = tmp.x;
for (int i = tmp.cur + 1; i < (int)gs[x].size(); i++) {
if (!vs[gs[x][i].first]) {
qs.push((node) { x, ds[x] + gs[x][i].second, i });
break;
}
}
int v = gs[x][tmp.cur].first;
if (vs[v])
continue;
vs[v] = 1, ds[v] = tmp.dis;
Ss.emplace_back(v);
if (vt[v]) {
z = v;
break;
}
if (gs[v].size())
qs.push((node) { v, ds[v] + gs[v][0].second, 0 });
} else {
node tmp = qt.top();
qt.pop();
int x = tmp.x;
for (int i = tmp.cur + 1; i < (int)gt[x].size(); i++) {
if (!vt[gt[x][i].first]) {
qt.push((node) { x, dt[x] + gt[x][i].second, i });
break;
}
}
int v = gt[x][tmp.cur].first;
if (vt[v])
continue;
vt[v] = 1, dt[v] = tmp.dis;
St.emplace_back(v);
if (vs[v]) {
z = v;
break;
}
if (gt[v].size())
qt.push((node) { v, dt[v] + gt[v][0].second, 0 });
}
}
if (z == -1) {
for (auto v : Ss) vs[v] = 0;
for (auto v : St) vt[v] = 0;
Ss.clear(), St.clear();
return -1;
}
int ans = ds[z] + dt[z];
for (auto v : Ss) {
int lim = (ds[z] - ds[v]) << 1;
for (auto e : gs[v]) {
if (e.second >= lim)
break;
if (vt[e.first])
ans = min(ans, ds[v] + dt[e.first] + e.second);
}
}
for (auto v : St) {
int lim = (dt[z] - dt[v]) << 1;
for (auto e : gt[v]) {
if (e.second >= lim)
break;
if (vs[e.first])
ans = min(ans, dt[v] + ds[e.first] + e.second);
}
}
for (auto v : Ss) vs[v] = 0;
for (auto v : St) vt[v] = 0;
Ss.clear(), St.clear();
return ans;
}
signed main() {
cin >> n >> m >> q >> seed;
mt19937 gen(seed);
vector<tuple<int, int, long long>> edges(m);
unsigned max = -1u / n * n;
auto sample = [&]() {
unsigned x;
do { x = gen(); } while(x >= max);
return x % n + 1;
};
for(auto & [u, v, w] : edges) {
u = sample();
v = sample();
w = gen();
gs[u].emplace_back(make_pair(v, w));
gt[v].emplace_back(make_pair(u, w));
// cout << u << " " << v << " " << w << " asdf\n";
}
for (int i = 1; i <= n; i++) {
sort(gs[i].begin(), gs[i].end(), [](pair<int, int> a, pair<int, int> b) { return a.second < b.second; });
sort(gt[i].begin(), gt[i].end(), [](pair<int, int> a, pair<int, int> b) { return a.second < b.second; });
}
while (q--) {
int s, t;
cin >> s >> t;
cout << Query(s, t) << "\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 5
Accepted
time: 2ms
memory: 17748kb
input:
4 8 5 1112792816 2 3 4 3 4 3 3 2 1 4
output:
3419197189 1798364963 1798364963 3986398077 2337967903
result:
ok 5 lines
Test #2:
score: 0
Wrong Answer
time: 8ms
memory: 18572kb
input:
2000 2000 2000 3336994405 659 1650 1678 341 818 235 1380 1865 1927 1366 1233 1673 267 1698 775 1022 1255 1110 1533 1928 1854 169 1579 729 449 1335 943 583 360 50 795 926 1584 911 1924 604 280 309 1429 420 1107 1858 1466 76 265 1109 1077 622 245 1941 957 1434 1560 1128 122 51 229 925 826 1006 851 323...
output:
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
result:
wrong answer 1925th lines differ - expected: '-1', found: '54140739830'
Subtask #2:
score: 0
Wrong Answer
Test #6:
score: 0
Wrong Answer
time: 575ms
memory: 250416kb
input:
3000 3000000 10000 37461678 2873 1368 1757 2000 1262 1822 2484 1778 2055 2096 2545 366 2923 2028 1469 1874 691 631 1173 2967 894 2020 1207 881 373 236 1913 1923 1351 16 1066 2032 471 1561 1047 2043 457 145 2728 1752 2521 1199 1568 904 2515 543 1472 2161 748 2744 748 1908 912 172 2340 2494 977 267 10...
output:
36084543 31412873 37798756 27805775 34147148 34794369 34246239 29266109 31284750 23367579 28024834 32364058 35148017 26615083 22117708 25983590 18599224 22331497 22413466 25257238 28851902 31401686 25715015 21508236 26347324 29954944 31303241 32785744 31005342 29736099 34131341 34561545 26819582 294...
result:
wrong answer 2nd lines differ - expected: '49860181', found: '31412873'
Subtask #3:
score: 0
Wrong Answer
Test #13:
score: 0
Wrong Answer
time: 118ms
memory: 34456kb
input:
200000 200000 10000 1824322211 104482 112162 130667 13436 36792 142259 51832 97549 15358 180076 128251 92635 45296 195115 62109 38014 22014 86754 79735 103777 94797 96086 196760 5955 45622 59618 12995 62585 55686 156402 23085 68138 170749 148553 97603 160274 112975 22651 116322 190720 84774 57075 23...
output:
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 154907644398 -1 -1 -1 -1 -1 -1 -1 -1 253794410196 -1 -1 -1 -1 -1 -1 -1 -1 331729802644 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
result:
wrong answer 19th lines differ - expected: '-1', found: '154907644398'
Subtask #4:
score: 0
Wrong Answer
Test #17:
score: 0
Wrong Answer
time: 2808ms
memory: 206528kb
input:
200000 500000 10000 3113327438 68816 31422 174349 125983 18111 188786 84806 87249 142007 180723 95611 116398 104758 196349 77547 89859 120350 77199 110906 10209 177461 194861 115505 105566 27493 166237 15676 158290 86204 116010 159979 125659 132461 61989 194289 157721 18830 82910 166696 98162 125208...
output:
21671419385 22830633117 20817520638 19295613250 -1 21432165357 -1 -1 20340897941 19365143518 -1 19676199705 21931873212 17892266742 19478702596 -1 -1 19634210670 20149023019 19519453983 21459028183 21810579894 -1 11676818440 22050791456 22525112818 22863622221 22955157614 23192579413 20340643363 226...
result:
wrong answer 2nd lines differ - expected: '-1', found: '22830633117'
Subtask #5:
score: 0
Wrong Answer
Test #21:
score: 0
Wrong Answer
time: 2596ms
memory: 205544kb
input:
200000 500000 10000 1843053063 3409 108359 168924 184622 13119 119837 109492 38050 97152 51201 49047 12472 183998 191613 193074 177289 194248 104409 15509 88499 61967 143398 4532 56790 196650 158711 63655 70744 140178 107299 63530 87330 127334 159237 7134 184418 125289 28604 176966 179527 181695 128...
output:
18098332289 19765996852 22666064981 20352379266 -1 22148006084 22209493371 21117534178 20148768590 21885827336 17793204212 13278636159 20293439473 18134229421 19738987477 20802733114 18907232578 -1 19353572589 20064052990 19250975264 17684395761 18892661205 15726744387 17925886800 18729273264 165787...
result:
wrong answer 2nd lines differ - expected: '22666064981', found: '19765996852'
Subtask #6:
score: 0
Wrong Answer
Test #24:
score: 0
Wrong Answer
time: 2682ms
memory: 392984kb
input:
100000 3000000 10000 3892765041 14843 34156 43390 49542 38564 95501 26194 87126 18638 53346 69414 47011 95472 58303 44370 77172 75652 90555 94386 31888 47911 9905 70599 97061 52764 24896 31445 15589 82314 43852 97155 93412 11834 45082 75614 42459 67802 32024 82389 4968 32860 62514 97630 28012 14839 ...
output:
1547972368 1533240012 1192488694 1395192333 1358041074 1526851240 1238074836 1613603538 1673462717 1527975914 1250925907 1501593728 1516870609 1379429801 876409061 1045313627 1435877170 1472430305 1493257461 1468431850 1429598050 1427885677 1568753790 1416358110 1539176715 1645644645 1355061676 1047...
result:
wrong answer 4th lines differ - expected: '1802115335', found: '1395192333'
Subtask #7:
score: 0
Wrong Answer
Test #33:
score: 0
Wrong Answer
time: 4041ms
memory: 543320kb
input:
200000 3000000 10000 3910662331 161257 40967 50546 86049 199665 199302 177403 36274 158790 143151 193304 78511 28032 149723 96394 37099 2157 76024 195400 34830 41933 147591 191613 96468 194837 67293 57992 63117 24749 6694 117818 87323 46130 53470 174812 24950 149173 124886 119910 54123 2297 124533 5...
output:
3371897180 3059012504 3366068379 3744057301 3645536227 3101758258 2859026100 2783692291 2819243872 3463309384 3126441478 3383785063 3045843635 3338388497 2938858489 3068009172 3286979519 3469230860 2946038594 3353365020 3560221847 2160161436 3395537940 3081806281 2746833185 3019128996 2984121970 340...
result:
wrong answer 3rd lines differ - expected: '3899803743', found: '3366068379'