QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#559818 | #8673. 最短路径 | -xcxxx- | 0 | 4077ms | 326024kb | C++14 | 3.8kb | 2024-09-12 09:32:22 | 2024-09-12 09:32:22 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
int rd() {int x=0,f=1;char c=getchar();while(!isdigit(c))f=(c=='-'?-1:f),c=getchar();while(isdigit(c))x=x*10+c-'0',c=getchar();return x*f;}
const int N=200005;
int n,m,q,seed;
struct node {int nd,dis,fr,pos;};
bool operator<(node a,node b) {return a.dis>b.dis;}
vector<node> G[N],R[N];
vector<int> alls,allt;
int vis[N],dis[N];
priority_queue<node> qs,qt;
void generate_edges(int n, int m, unsigned seed) {
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();
G[u].push_back({v,w,0,0});
R[v].push_back({u,w,0,0});
} // u 到 v 存在边权为 w 的边
}
signed main() {
n=rd(),m=rd(),q=rd(),seed=rd();
generate_edges(n,m,seed);
for(int i=1;i<=n;i++) sort(G[i].begin(),G[i].end()),reverse(G[i].begin(),G[i].end());
for(int i=1;i<=n;i++) sort(R[i].begin(),R[i].end()),reverse(R[i].begin(),R[i].end());
// for(int i=1;i<=n;i++) for(auto t:G[i]) fprintf(stderr,"%lld %lld %lld\n",i,t.nd,t.dis);
memset(dis,-1,sizeof(dis));
while(q--) {
int s=rd(),t=rd(),meet=-1,diss=-1,dist=-1,ans=0x3f3f3f3f3f3f3f3f;
qs.push({s,0,0,0}),qt.push({t,0,0,0});
while(!qs.empty()||!qt.empty()) {
if(!qs.empty()) {
int u=qs.top().nd,fr=qs.top().fr,pos=qs.top().pos,d=qs.top().dis;qs.pop();
// printf("!! u=%lld d=%lld dis=%lld\n",u,d,dis[u]);
if(vis[u]==2) {
vis[u]=3;
alls.push_back(u);
meet=u;
diss=d,dist=dis[u];
ans=diss+dist;
break;
}
if(dis[u]!=-1) continue;
alls.push_back(u);
vis[u]=1;
dis[u]=d;
if(fr&&pos<G[fr].size()-1) qs.push({G[fr][pos+1].nd,dis[fr]+G[fr][pos+1].dis});
if(!G[u].empty()) qs.push({G[u][0].nd,dis[u]+G[u][0].dis,u,0});
}
if(!qt.empty()) {
int u=qt.top().nd,fr=qt.top().fr,pos=qt.top().pos,d=qt.top().dis;qt.pop();
// printf("@@ u=%lld d=%lld\n",u,d);
if(vis[u]==1) {
vis[u]=3;
allt.push_back(u);
meet=u;
diss=dis[u],dist=d;
ans=diss+dist;
break;
}
if(dis[u]!=-1) continue;
allt.push_back(u);
vis[u]=2;
dis[u]=d;
if(fr&&pos<R[fr].size()-1) qt.push({R[fr][pos+1].nd,dis[fr]+R[fr][pos+1].dis});
if(!R[u].empty()) qt.push({R[u][0].nd,dis[u]+R[u][0].dis,u,0});
}
}
if(meet==-1) {
puts("-1");
continue;
}
// printf("dis: %d -> ",s);
// for(int u:alls) if(u!=meet) printf("(%lld,%lld) ",u,dis[u]);puts("");
// printf("dis: %d -> ",t);
// for(int u:allt) if(u!=meet) printf("(%lld,%lld) ",u,dis[u]);puts("");
// printf("meet=%lld diss=%lld dist=%lld\n",meet,diss,dist);
for(int u:alls) for(auto t:G[u]) if((vis[t.nd]&2)&&t.dis<=2*(diss-dis[u])) ans=min(ans,(u==meet?diss:dis[u])+t.dis+(t.nd==meet?dist:dis[t.nd]));
for(int u:allt) for(auto t:R[u]) if((vis[t.nd]&1)&&t.dis<=2*(dist-dis[u])) ans=min(ans,(u==meet?dist:dis[u])+t.dis+(t.nd==meet?diss:dis[t.nd]));
printf("%lld\n",ans);
for(int u:alls) dis[u]=-1,vis[u]=0;
for(int u:allt) dis[u]=-1,vis[u]=0;
alls.clear(),allt.clear(),qs=qt=priority_queue<node>();
}
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: 0ms
memory: 16252kb
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: 16208kb
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 385298762 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 15263341283 -1 -1 -1 -1 -1 -1 -1 -1 -1 21865715553 -1 -1 9924386151 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 23233999979 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 4962463767 -1 -1 -1 -1 1630688998...
result:
wrong answer 14th lines differ - expected: '-1', found: '385298762'
Subtask #2:
score: 0
Wrong Answer
Test #6:
score: 0
Wrong Answer
time: 4054ms
memory: 258500kb
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 51219376 27805775 41392651 50932259 43130013 40352182 37675345 40680710 37276839 37672078 50703016 26615083 25983590 51209814 51989594 31222991 43288260 37020730 38702764 62523556 34199475 45804995 37077614 39809073 48340990 63207141 31005342 51960568 35033769 39515332 46677334 378...
result:
wrong answer 3rd lines differ - expected: '45803385', found: '51219376'
Subtask #3:
score: 0
Wrong Answer
Test #13:
score: 0
Wrong Answer
time: 60ms
memory: 33728kb
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:
wrong answer 184th lines differ - expected: '-1', found: '20984484831'
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:
32775202143 -1 48342034689 19295613250 -1 29591625645 -1 30633332494 32024231177 28043135986 -1 -1 46748559106 35132170862 21104294718 -1 24089923614 -1 26899942151 23727970709 32329362802 39577484096 -1 30060718651 27987383339 29065653090 -1 32621275766 26871322881 -1 29706891089 28857696662 -1 443...
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:
36783709652 27670390662 27427872677 27064793229 -1 44905979737 26676428133 26455642074 29489555581 38157421078 30042096924 24723572599 40423774869 23892588196 30985942883 25178681468 25532074688 -1 35258795701 33207638360 23208160613 26047036449 29305308240 30847435482 29364987473 30243114277 165787...
result:
Subtask #6:
score: 0
Wrong Answer
Test #24:
score: 0
Wrong Answer
time: 3387ms
memory: 325368kb
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:
2358893928 1533240012 1192488694 2371448451 1770164590 2275365832 2582327740 1762089620 2355349061 2648748729 1551435244 1967819596 2630004863 2067856586 2334169728 1877583272 1953277253 2657880286 2289732457 2413784346 2027352598 2110220096 2745104837 1582695830 1940165963 2462020161 1732043264 231...
result:
wrong answer 1st lines differ - expected: '1547972368', found: '2358893928'
Subtask #7:
score: 0
Wrong Answer
Test #33:
score: 0
Wrong Answer
time: 4077ms
memory: 326024kb
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:
3461948021 4430448560 5126091583 5110985359 4509626594 5025166605 4674172321 4683332281 4789603979 5306373899 3438482735 4549246464 3591036762 3768851308 3201003504 6279794833 6463581239 5357865879 4723331645 5286297442 4980581641 4585587763 5729303284 5015090696 4203693705 3864513044 5266642750 478...
result:
wrong answer 1st lines differ - expected: '3371897180', found: '3461948021'