QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#560166 | #8673. 最短路径 | Hridaya | 0 | 3895ms | 198980kb | C++14 | 6.2kb | 2024-09-12 13:50:39 | 2024-09-12 13:50:40 |
Judging History
answer
// Writer: Hridaya Time:2024.9.12 By:NFLS
/********************************************/
// #pragma GCC optimize(3,"Ofast","inline")
// #include <iostream> // cin, cout, cerr, getline
#include <bits/stdc++.h>
// #include <utility> // pair, make_pair, swap, move
// #include <ctime> // clock, time, difftime, tm, localtime
// #include <unordered_map> // unordered_map, unordered_multimap
// #include <unordered_set> // unordered_set, unordered_multiset
// #include <algorithm> // sort, find, reverse, binary_search, max_element, min_element
// #include <ios> // ios_base, ios_base::failure, ios::sync_with_stdio
// #include <vector> // vector, vector::push_back, vector::size, vector::at
// #include <bitset> // bitset, bitset::set, bitset::reset
// #include <cstring> // memset, memcpy, strcmp, strlen
// #include <map> // map, multimap, map::insert
// #include <set> // set, multiset, set::insert
// #include <queue> // queue, priority_queue
// #include <stack> // stack, stack::push, stack::pop
// #include <deque> // deque, deque::push_back, deque::pop_front
// #include <list> // list, list::push_back, list::remove
// #include <cmath> // sqrt, pow, sin, cos, log
// #include <cstddef> // size_t, ptrdiff_t
// #include <cstdint> // int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t, uint32_t, uint64_t
// #include <climits> // INT_MAX, CHAR_MIN
// #include <iterator> // iterator, advance, next, prev
// #include <chrono> // chrono::steady_clock, chrono::system_clock, chrono::duration
// #include <fstream> // ifstream, ofstream, fstream
using namespace std;
typedef long long ll;
#define foru(i,x,y) for (register int i(x); i <= y; ++i)
#define ford(i,x,y) for (register int i(x); i >= y; --i)
#define Linf 9223372036854775807
#define Iinf 2147483647
#define Sinf 32767
namespace IO {
char ch ('~');
template<typename Type>
inline Type read() {
Type x(0);
bool m(0);
while (!isdigit(ch)) {
m |= (ch == '-');
ch = getchar(); }
while (isdigit(ch)) {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar(); }
return m ? (~x + 1) : x; }
template<typename Type>
inline void write(Type x) {
if (x < 0) putchar('-'), x = (~x + 1);
if (x > 9) write(x / 10);
putchar(x % 10 ^ 48); }
template<typename Type>
inline void writes(Type x, char c) {
write(x), putchar(c); }
#define read() read<int>()
#define Jianghu_bloke \
freopen("QwQ.in", "r", stdin); freopen("QwQ.out", "w", stdout);
} using namespace IO;
#define pb emplace_back
#define fi first
#define se second
const int N (2e5 + 10);
struct edge {
int u, v;
ll w; };
std::vector<edge> generate_edges(int n, int m, unsigned seed) {
std::mt19937 gen(seed);
std::vector<edge> edges(m);
unsigned max = -1u / n * n;
auto sample = [&]() {
unsigned x;
do {
x = gen(); }
while (x >= max);
return x % n + 1; };
for (auto&e : edges) {
e.u = sample();
e.v = sample();
e.w = gen(); }
return edges; }
int n, m, q;
ll seed;
vector<pair<int, ll>>e[N], re[N];
ll dis[N];
int lst[N];
std :: vector<int> road[N];
ll lstw[N];
inline void dijkstra(int pos) {
priority_queue<pair<ll, int>>pq;
pq.push({0, pos });
foru(i, 1, n) dis[i] = 1e18;
vector<int> vis(n + 1);
dis[pos] = 0;
while (!pq.empty()) {
auto u = pq.top().se; pq.pop();
if (vis[u])continue;
if (lst[u]) road[u] = road[lst[u]], road[u].pb(u);
vis[u] = 1;
for (auto&E : e[u]) {
int to (E.fi);
if (dis[to] > dis[u] + E.se) {
dis[to] = dis[u] + E.se;
pq.push({-dis[to], to });
lstw[to] = E.se;
lst[to] = u; } } } }
ll dis1[N], dis2[N];
int vis[N], que[N], all, seg1[N], seg2[N], use[N];
mt19937_64 rnd;
inline int rad(int l, int r) {
return rnd() % (r - l + 1) + l; }
const int xN = 1 << 18;
inline void upd1(int x) {
seg1[x + xN] = x;
for (int u = (x + xN) >> 1; u; u >>= 1) {
if (dis1[x] > dis1[seg1[u]]) break;
seg1[u] = x; } }
inline void del1(int x) {
seg1[x + xN] = 0;
for (int u = (x + xN) >> 1; u; u >>= 1)
seg1[u] = dis1[seg1[u * 2]] < dis1[seg1[u * 2 + 1]] ? seg1[u * 2] : seg1[u * 2 + 1]; }
inline void upd2(int x) {
seg2[x + xN] = x;
for (int u = (x + xN) >> 1; u; u >>= 1) {
if (dis2[x] > dis2[seg2[u]]) break;
seg2[u] = x; } }
inline void del2(int x) {
seg2[x + xN] = 0;
for (int u = (x + xN) >> 1; u; u >>= 1)
seg2[u] = dis2[seg2[u * 2]] < dis2[seg2[u * 2 + 1]] ? seg2[u * 2] : seg2[u * 2 + 1]; }
int ban1[N], ban2[N];
const int M = 1 << 18;
namespace Hridaya {
inline void solve() {
int x (read()), y(read());
if (x == y) return puts("0"), void();
all = 2, que[1] = x, que[2] = y, vis[x] = vis[y] = 1;
dis1[x] = 0, dis2[y] = 0;
upd1(x), upd2(y);
ll ans (Linf);
while (1) {
if (ans > 1e17 && (dis1[seg1[1]] > 1e17 || dis2[seg2[1]] > 1e17)) break;
if (min(dis1[seg1[1]], dis2[seg2[1]]) * 2 >= ans) break;
if (dis1[seg1[1]] < dis2[seg2[1]]) {
int u = seg1[1];
del1(u);
for (auto&E : e[u]) {
int to = E.fi;
if (dis1[to] > dis1[u] + E.se) {
dis1[to] = dis1[u] + E.se;
if (!vis[to]) vis[to] = 1, que[++all] = to;
else ans = min(ans, dis1[to] + dis2[to]);
if (dis1[to] * 2 < ans) upd1(to); } } }
else {
int u = seg2[1];
del2(u);
for (auto&E : re[u]) {
int to = E.fi;
if (dis2[to] > dis2[u] + E.se) {
dis2[to] = dis2[u] + E.se;
if (!vis[to]) vis[to] = 1, que[++all] = to;
else ans = min(ans, dis1[to] + dis2[to]);
if (dis2[to] * 2 < ans) upd2(to); } } } }
foru(i, 1, all) dis1[que[i]] = dis2[que[i]] = 1e18, vis[que[i]] = 0;
writes(ans ^ Linf ? ans : -1, '\n'); }
signed main() {
n = read(), m = read(), q = read(), seed = read();
auto E = generate_edges(n, m, seed);
sort(E.begin(), E.end(), [&] (edge a, edge b) {
return a.w < b.w; });
for (auto&ed : E) if (ed.u != ed.v) e[ed.u].pb(ed.v, ed.w), re[ed.v].pb(ed.u, ed.w);
foru(i, 0, n) dis1[i] = dis2[i] = 1e18;
while (q--) solve();
return 0; }
// Main Space
}
signed main() {
// Jianghu_bloke
Hridaya :: main();
// Coding Completed!
return 0; }
详细
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 5
Accepted
time: 0ms
memory: 24432kb
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: 0ms
memory: 27028kb
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 128th lines differ - expected: '-1', found: '1000000002601140368'
Subtask #2:
score: 0
Wrong Answer
Test #6:
score: 15
Accepted
time: 3895ms
memory: 198980kb
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 49860181 45803385 27805775 41392651 43506617 39517515 39687913 37675345 23367579 37276839 32364058 50703016 26615083 25983590 51209814 42921191 31222991 39092809 25257238 36267824 60108033 34199475 45804995 35826034 34257048 38718065 55135658 31005342 41408425 35033769 37667712 42873640 378...
result:
ok 10000 lines
Test #7:
score: 15
Accepted
time: 2888ms
memory: 157168kb
input:
3000 2000000 10000 2522167365 2102 2825 724 1689 2259 2561 1681 677 62 2183 2589 1214 926 1138 674 2610 1679 1607 1349 2461 2617 1599 457 2347 584 518 1506 554 2954 470 1027 893 1924 2 2624 2746 1366 2651 2236 2085 362 2871 1413 1763 2497 404 1507 1216 894 322 2221 2553 824 2374 1883 1507 2484 2504 ...
output:
65574243 49955828 53828505 51865209 52351116 61557386 51116830 55590246 56377606 32235042 40593621 48849551 65887052 65047947 68965925 45241121 29819326 68037564 51238828 51815122 51454820 50482802 78004899 69718038 51304835 72570590 63002470 71137709 72879314 39737181 46218127 56704281 46947435 745...
result:
ok 10000 lines
Test #8:
score: 15
Accepted
time: 1832ms
memory: 97616kb
input:
3000 1000000 10000 711905757 844 1281 882 1379 1448 2597 2686 1871 1556 677 337 871 825 248 1686 345 1259 775 422 763 2445 2585 1514 1028 90 1993 2203 2185 2965 2115 499 2266 2274 2635 713 450 2978 1453 1745 1010 11 350 1746 2622 1070 1458 438 1462 2936 2707 1797 2495 1929 873 1426 32 1696 548 2756 ...
output:
137380549 162704262 143745916 115032641 79062560 136541282 75207874 55127915 100171107 113209549 114113337 128511651 121886243 151535892 106186341 124611628 123504840 127411130 157283803 92948750 154286595 124377360 88897895 191915816 111939138 111074921 99047774 95249923 136436236 57049943 93591345...
result:
ok 10000 lines
Test #9:
score: 15
Accepted
time: 1172ms
memory: 59976kb
input:
3000 500000 10000 4065069523 1355 22 595 1315 137 828 444 1241 483 1807 1852 377 1292 2452 478 1758 2712 2071 2243 1344 194 2765 2645 1718 2078 202 1860 2607 495 1091 2492 2800 2594 694 2021 2441 1393 1253 1378 2008 114 727 1019 196 1142 71 2787 2507 650 2675 2074 2132 2697 614 1611 1662 2687 358 13...
output:
283099212 197991417 240849607 272997490 378109456 160014053 252448699 281163198 280701476 178120202 189979017 272229633 267521047 219833816 183204444 275985942 208578258 148366474 287620336 264106800 220537155 167544642 306771926 200838815 179562301 313150724 246238367 194938277 197389047 201592436 ...
result:
ok 10000 lines
Test #10:
score: 15
Accepted
time: 432ms
memory: 34680kb
input:
3000 100000 10000 2346395888 2334 174 757 2882 2571 2749 2571 1300 1511 2435 170 1648 107 465 2588 2135 1571 1754 2919 2295 717 129 1779 2941 1493 1505 1784 470 164 371 1381 1204 1644 1556 2234 1583 54 2836 815 777 1060 671 1147 1945 879 2968 2030 609 770 2226 2414 1944 1893 885 478 1705 643 439 135...
output:
1037539058 841259924 1119227208 936606501 1124792817 550785284 1187414290 1105329599 534927835 1079864539 1056661616 1426806296 1387193176 717428828 1083183267 793850415 455433261 527722167 1087705276 1140313309 1048197735 777649783 1066670304 1244984113 1535939812 1008629859 1033454877 1242231980 1...
result:
ok 10000 lines
Test #11:
score: 0
Wrong Answer
time: 7ms
memory: 27280kb
input:
3000 3000 10000 397949456 418 2179 1809 996 1420 2230 204 2974 2416 2274 2601 2425 172 1604 263 2652 2446 2508 1807 1321 1619 2575 1918 735 201 2718 134 1960 2804 22 189 988 1949 39 2260 2933 22 1853 2721 761 911 2218 2189 1676 2461 2594 471 643 1645 1453 144 1601 2501 1592 53 1710 1452 596 352 2347...
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 526th lines differ - expected: '-1', found: '1000000002985388956'
Subtask #3:
score: 0
Time Limit Exceeded
Test #13:
score: 0
Time Limit Exceeded
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:
result:
Subtask #4:
score: 0
Time Limit Exceeded
Test #17:
score: 0
Time Limit Exceeded
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:
result:
Subtask #5:
score: 0
Time Limit Exceeded
Test #21:
score: 0
Time Limit Exceeded
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:
result:
Subtask #6:
score: 0
Time Limit Exceeded
Test #24:
score: 0
Time Limit Exceeded
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:
result:
Subtask #7:
score: 0
Time Limit Exceeded
Test #33:
score: 0
Time Limit Exceeded
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...