QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#560245 | #8673. 最短路径 | AllenJYL | 10 | 5988ms | 181640kb | C++14 | 4.6kb | 2024-09-12 14:33:05 | 2024-09-12 14:33:06 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
// #define int128 __int128_t
#define ls p<<1
#define rs p<<1|1
#define msp(_array) memset(_array,0x3f,sizeof _array)
#define ms0(_array) memset(_array,0,sizeof _array)
#define msn(_array) memset(_array,-1,sizeof _array)
#define mc(_tar,_array) memcpy(_tar,_array,sizeof _tar)
#define pii pair<int,int>
#define endl '\n'
bool bg_memory;
const int mod=(1ll<<60)-1;
const int inf=1e18;
const int bs=23333;
const double eps=1e-6;
const int N=2e5+7,M=3e6+7;
inline void cmax(int &a,int b){a=a<b?b:a;}
inline void cmin(int &a,int b){a=a>b?b:a;}
int n,m,q,seed;
int s,t,o;
vector<pii> e[N],r[N];
int dis[N][3],cl[N][3];
int ans;
set<int> ap;
void generate_edges(){
std::mt19937 gen(seed);
unsigned max = -1u / n * n;
auto sample = [&]() {
unsigned x;
do { x = gen(); } while(x >= max);
return x % n + 1;
};
for(int i=1;i<=m;i++){
int u=sample();
int v=sample();
int w=gen();
e[u].push_back({v,w});
r[v].push_back({u,w});
}
}
priority_queue<pair<pii,int> > ps,pt;
void dij(){
for(int i:ap) cl[i][1]=cl[i][2]=0;
ap.clear();
dis[s][1]=dis[t][2]=0;
cl[s][1]=cl[t][2]=1;
while(ps.size()) ps.pop();
while(pt.size()) pt.pop();
ps.push({{-e[s][0].second,s},0});
pt.push({{-r[t][0].second,t},0});
ap.insert(s);
ap.insert(t);
while(ps.size()&&pt.size()){
pair<pii,int> x;
if(ps.size()<=pt.size()){
x=ps.top();
x.first.first=-x.first.first;
ps.pop();
for(int j=x.second+1;j<e[x.first.second].size();j++){
pii i=e[x.first.second][j];
if(!cl[i.first][1]){
ps.push({{-dis[x.first.second][1]-i.second,x.first.second},j});
break;
}
}
int v=e[x.first.second][x.second].first;
if(cl[v][1]) continue;
cl[v][1]=1;
dis[v][1]=x.first.first;
ap.insert(v);
if(cl[v][2]) return o=v,void();
if(e[v].size()) ps.push({{-dis[v][1]-e[v][0].second,v},0});
}else{
x=pt.top();
x.first.first=-x.first.first;
pt.pop();
for(int j=x.second+1;j<r[x.first.second].size();j++){
pii i=r[x.first.second][j];
if(!cl[i.first][2]){
pt.push({{-dis[x.first.second][2]-i.second,x.first.second},j});
break;
}
}
int v=r[x.first.second][x.second].first;
if(cl[v][2]) continue;
cl[v][2]=1;
dis[v][2]=x.first.first;
ap.insert(v);
if(cl[v][1]) return o=v,void();
if(r[v].size()) pt.push({{-dis[v][2]-r[v][0].second,v},0});
}
}
}
mt19937 rnd(time(0));
int Case=1;
void Main(){
cin>>n>>m>>q>>seed;
generate_edges();
for(int i=1;i<=n;i++) sort(e[i].begin(),e[i].end(),[](pii a,pii b){return a.second<b.second;});
for(int i=1;i<=n;i++) sort(r[i].begin(),r[i].end(),[](pii a,pii b){return a.second<b.second;});
msp(dis);
for(int i=1;i<=q;i++){
cin>>s>>t;
if(s==t){
cout<<0<<endl;
continue;
}
if(e[s].empty()||r[t].empty()){
cout<<-1<<endl;
continue;
}
o=0;
dij();
if(!o){
cout<<-1<<endl;
continue;
}
ans=dis[o][1]+dis[o][2];
for(int j:ap){
if(cl[j][1]){
for(pii k:e[j]){
if(k.second>=2*(dis[o][1]-dis[j][1])) break;
if(cl[k.first][2]) cmin(ans,dis[j][1]+dis[k.first][2]+k.second);
}
}
}
cout<<ans<<endl;
}
return;
}
string RdFile="c";
bool en_memory;
signed main(){
auto bg_clock=chrono::high_resolution_clock::now();
#ifdef ONLINE_JUDGE
// freopen((RdFile+".in").c_str(),"r",stdin);
// freopen((RdFile+".out").c_str(),"w",stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// cin>>Case;
while(Case--) Main();
auto en_clock=chrono::high_resolution_clock::now();
auto duration_clock=chrono::duration_cast<chrono::microseconds>(en_clock-bg_clock);
#ifndef ONLINE_JUDGE
cerr<<endl<<"code_time_used: "<<duration_clock.count()*0.001<<"ms"<<endl;
cerr<<"code_memory_used: "<<(&en_memory-&bg_memory)/1024.0/1024<<"MB"<<endl;
#endif
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: 4ms
memory: 18144kb
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: 5
Accepted
time: 6ms
memory: 19064kb
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:
ok 2000 lines
Test #3:
score: 0
Wrong Answer
time: 22ms
memory: 18696kb
input:
1000 2000 2000 1526732796 400 914 837 927 7 271 873 60 934 156 981 329 973 512 276 54 540 278 605 230 681 555 636 706 955 618 640 214 859 696 267 595 38 839 309 12 484 919 746 49 948 337 607 638 438 163 817 869 95 518 534 376 369 331 665 64 736 970 154 41 510 425 876 907 143 803 270 403 350 286 131 ...
output:
14198403396 -1 20203456441 11552404306 16160464812 27144556597 -1 5570702410 -1 19513776618 10597134504 8945453029 20326028889 -1 12608727274 17050357023 -1 -1 15134668738 19589312812 32078322699 16255615559 -1 20150114514 15492954694 -1 5265380455 -1 19291857101 -1 -1 -1 19427108277 17619903738 -1 ...
result:
wrong answer 25th lines differ - expected: '15485138820', found: '15492954694'
Subtask #2:
score: 0
Wrong Answer
Test #6:
score: 0
Wrong Answer
time: 688ms
memory: 141688kb
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 48986450 27805775 41392651 43506617 39517515 40352182 37675345 23367579 37276839 32364058 50703016 26615083 25983590 51209814 42921191 31222991 39092809 25257238 37822632 61379702 34199475 45804995 37077614 34257048 38718065 55135658 31005342 41408425 35033769 37667712 42873640 378...
result:
wrong answer 3rd lines differ - expected: '45803385', found: '48986450'
Subtask #3:
score: 10
Accepted
Test #13:
score: 10
Accepted
time: 57ms
memory: 33236kb
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 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -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:
ok 10000 lines
Test #14:
score: 10
Accepted
time: 18ms
memory: 28208kb
input:
200000 100000 10000 1394653802 99794 128174 196511 141958 176353 6707 19037 95308 12331 132159 47825 12373 47277 130874 165656 114428 81800 12371 165878 128160 33280 71225 139344 138789 126396 182051 103407 151857 20873 18698 155652 38063 150807 191146 57310 174863 114490 88197 158133 29636 137962 1...
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:
ok 10000 lines
Test #15:
score: 10
Accepted
time: 28ms
memory: 26948kb
input:
100000 100000 10000 913053279 28316 35031 36768 9164 74111 12192 71120 23394 97477 34141 50880 24433 99500 23365 99785 571 95784 50853 8313 70744 33410 27807 29073 96498 82964 79943 32999 84423 90798 98756 98245 89258 89589 49557 90152 40866 53406 41385 33889 39018 42199 52421 13784 26639 85311 5769...
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:
ok 10000 lines
Test #16:
score: 10
Accepted
time: 0ms
memory: 18652kb
input:
1 1 10000 1920830832 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 10000 lines
Subtask #4:
score: 0
Wrong Answer
Test #17:
score: 0
Wrong Answer
time: 3882ms
memory: 48760kb
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 -1 32320841327 19295613250 -1 25762674206 -1 -1 30333017011 19365143518 -1 -1 33507263304 23396138679 19478702596 -1 -1 -1 20149023019 23727970709 24321329479 28875639856 -1 22877254445 25605430611 27724721382 -1 26550979061 25161604327 -1 22768163518 19348763468 -1 24220635647 161988758...
result:
wrong answer 3rd lines differ - expected: '31996366393', found: '32320841327'
Subtask #5:
score: 0
Wrong Answer
Test #21:
score: 0
Wrong Answer
time: 4049ms
memory: 48784kb
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 22666064981 23549058925 27064793229 -1 23116762056 22209493371 21117534178 22029252897 33988361853 17793204212 13278636159 25843769632 18134229421 29623865096 23847021502 20878297870 -1 -1 21042457357 23208160613 19615484227 26566774108 15726744387 23457868594 23517057425 16578768343 242...
result:
wrong answer 4th lines differ - expected: '26339412859', found: '27064793229'
Subtask #6:
score: 0
Wrong Answer
Test #24:
score: 0
Wrong Answer
time: 4146ms
memory: 178544kb
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 1802115335 1491444021 1888896300 1720188008 1762089620 1815841406 1831208977 1250925907 1756812381 2027344758 1385409721 1937527554 1881731891 1632784703 2090242303 1694524102 1864951243 1429598050 1599437722 2286394605 1416358110 1929044811 2024780259 1487757623 156...
result:
wrong answer 16th lines differ - expected: '1877583272', found: '1881731891'
Subtask #7:
score: 0
Wrong Answer
Test #33:
score: 0
Wrong Answer
time: 5988ms
memory: 181640kb
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 3899803743 5021405654 3885338786 3101758258 4211432244 3700678845 3489714730 4020388668 3314213999 4156507838 3045843635 3405479069 3201003504 4511026914 4847106102 3502897631 3579081638 3470448652 4525536546 2160161436 4372954162 3841655899 3367608876 3864513044 4225021719 377...
result:
wrong answer 4th lines differ - expected: '4918664465', found: '5021405654'