QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#561161 | #8673. 最短路径 | Juefan | 10 | 5550ms | 225764kb | C++20 | 3.1kb | 2024-09-12 20:51:43 | 2024-09-12 20:51:43 |
Judging History
answer
#include<bits/stdc++.h>
#define F(i,a,b) for(int i=a,i##E=b;i<=i##E;i++)
#define UF(i,a,b) for(int i=a,i##E=b;i>=i##E;i--)
#define sz(x) int(x.size())
using namespace std;typedef long long ll;
#define vec vector
template<typename T>
void operator+=(vec<T> &a,const T&b) {a.push_back(b);}
#define gc() getchar()
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
//char *p1,*p2,buf[1<<21];
unsigned read() {
unsigned s=0,w=0;char ch=gc();
while(ch<'0'||ch>'9') w|=(ch=='-'),ch=gc();
while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48),ch=gc();
return w?-s:s;
}
std::vector<std::tuple<int, int, long long>> generate_edges(int n, int m, unsigned seed) {
std::mt19937 gen(seed);
std::vector<std::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();
} // u 到 v 存在边权为 w 的边
return edges;
} const int N=2e5+5;
const ll inf=1e18;
int n,m,q;
struct E{int v;ll w;bool operator<(E b)const{return w<b.w;}};
vec<E> G[N][2];
using T=tuple<ll,int,int>;
ll d[N][2];int now[N][2],vis[N][2];
ll sol(int s,int t) {
if(s==t) return 0;
if(!sz(G[s][0])||!sz(G[t][1])) return -1;
priority_queue<T,vec<T>,greater<T>> q[2];
auto Push=[&](int x,int i,int r) {
if(i>=sz(G[x][r])) return;
auto[v,w]=G[x][r][i];
if(d[x][r]+w<d[v][r]) q[r].push({d[v][r]=d[x][r]+w,v,x});
else q[r].push({d[v][r],v,x});
};
d[s][0]=0;d[t][1]=0;
vis[s][0]=vis[t][1]=1;
Push(s,0,0);Push(t,0,1);
vec<int> V[2];
int X=-1;V[0]+=s;V[1]+=t;ll ans=inf;
while(sz(q[0])&&sz(q[1])) {
int r=sz(V[0])>sz(V[1]);
auto[D,x,p]=q[r].top();q[r].pop();
Push(p,++now[p][r],r);
if(D>d[x][r]) continue;
vis[x][r]=1;V[r]+=x;
if(vis[x][0]&&vis[x][1]) {/*cerr<<"X:"<<x<<endl;*/X=x;ans=d[x][0]+d[x][1];break;}
Push(x,0,r);
} //cerr<<"SZ:"<<sz(V[0])<<' '<<sz(V[1])<<endl;
#define x X
if(~x) {
for(int y:V[0]) for(auto[v,w]:G[y][0])
if(w<2*(d[x][0]-d[y][0])) ans=min(ans,d[y][0]+w+d[v][1]);
else break;
for(int y:V[1]) for(auto[v,w]:G[y][1])
if(w<2*(d[x][1]-d[y][1])) ans=min(ans,d[y][1]+w+d[v][0]);
else break;
}
auto clr=[&](int x) {d[x][0]=d[x][1]=inf;now[x][0]=now[x][1]=0;vis[x][0]=vis[x][1]=0;};
for(int y:V[0]) clr(y);
for(int y:V[1]) clr(y);
while(sz(q[0])) {
auto[D,x,p]=q[0].top();q[0].pop();
clr(x);clr(p);
}
while(sz(q[1])) {
auto[D,x,p]=q[1].top();q[1].pop();
clr(x);clr(p);
}
return ans>=inf?-1:ans;
#undef x
}
unsigned seed;
int main() {
n=read();m=read();q=read();
auto Edges=generate_edges(n,m,seed=read());
for(auto[u,v,w]:Edges) G[u][0]+=E{v,w},G[v][1]+=E{u,w};//,cerr<<"K::"<<u<<' '<<v<<' '<<w<<endl;
F(i,1,n) F(j,0,1) sort(begin(G[i][j]),end(G[i][j]));
F(j,0,1) F(i,1,n) d[i][j]=inf;
// if(n>2000) return 0;
F(i,1,q) {
int x=read(),y=read();
// if(seed==1526732796u) {
// if(i==66) cout<<x<<' '<<y<<endl;
// } else
cout<<sol(x,y)<<'\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Time Limit Exceeded
Test #1:
score: 5
Accepted
time: 2ms
memory: 5744kb
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: 2ms
memory: 6036kb
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
Time Limit Exceeded
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:
result:
Subtask #2:
score: 0
Time Limit Exceeded
Test #6:
score: 0
Time Limit Exceeded
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:
result:
Subtask #3:
score: 10
Accepted
Test #13:
score: 10
Accepted
time: 57ms
memory: 32564kb
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: 26ms
memory: 25992kb
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: 34ms
memory: 18540kb
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: 2ms
memory: 5784kb
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
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: 20
Accepted
time: 3971ms
memory: 57644kb
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 26339412859 -1 23116762056 22209493371 21117534178 22029252897 33952599088 17793204212 13278636159 25843769632 18134229421 29623865096 23847021502 20878297870 -1 -1 21042457357 23208160613 19615484227 26566774108 15726744387 23457868594 23352911380 16578768343 242...
result:
ok 10000 lines
Test #22:
score: 0
Time Limit Exceeded
input:
150000 500000 10000 1171171831 91544 80638 88533 64906 57189 104784 102394 38679 38414 49143 137564 139395 140558 140970 64039 53145 108163 56995 38924 95572 48927 4259 148782 85367 44887 19484 36838 83718 10792 128933 90704 81675 116605 19578 51500 21602 137498 101466 7037 146764 116437 62263 46477...
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:
1547972368 1533240012 1192488694 1802115335 1491444021 1888896300 1720188008 1762089620 1815841406 1831208977 1250925907 1756812381 2027344758 1385409721 1937527554 1877583272 1632784703 2090242303 1694524102 1818975564 1429598050 1599437722 2286394605 1416358110 1929044811 2022891575 1487757623 156...
result:
Subtask #7:
score: 0
Time Limit Exceeded
Test #33:
score: 20
Accepted
time: 5383ms
memory: 225764kb
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 4918664465 3834117056 3101758258 4211432244 3700678845 3320322266 4020388668 3314213999 4156507838 3045843635 3379778417 3201003504 4511026914 4847106102 3502897631 3579081638 3470448652 4322207527 2160161436 4372954162 3841655899 3367608876 3864513044 4225021719 377...
result:
ok 10000 lines
Test #34:
score: 20
Accepted
time: 5550ms
memory: 154044kb
input:
200000 2000000 10000 2100400722 62191 109424 121705 6930 25558 16338 40896 18277 89580 91941 101052 38359 70931 193129 64894 4539 176145 124000 35472 141029 47953 56977 55967 69742 181344 13576 134393 21044 147760 104339 94483 20695 74776 75910 72389 23358 8081 158392 137419 172752 142139 187012 461...
output:
6283629982 4810712484 6173371551 5572267087 4533360250 6948138461 3763810206 6015991679 5138843815 5882103239 4651947070 5141390603 6054389311 7266250132 4406869907 5633307251 4787973112 5399509441 5723882182 4777557240 6651824180 3628172060 5633580623 7288229990 6224865765 6453345185 5384740659 464...
result:
ok 10000 lines
Test #35:
score: 0
Time Limit Exceeded
input:
200000 1000000 10000 290139113 138932 153688 168671 103620 18747 33374 112901 24472 85778 40732 176095 174016 56934 101943 66099 106570 117429 196168 75545 14523 29780 55964 177216 75719 176362 159858 2281 11675 29555 177792 46955 145556 46525 122543 145774 189061 66990 159194 154928 91381 25084 494...