QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#762702 | #8232. Yet Another Shortest Path Query | 275307894a | TL | 968ms | 327832kb | C++14 | 2.6kb | 2024-11-19 16:16:57 | 2024-11-19 16:17:04 |
Judging History
answer
#include<bits/stdc++.h>
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define eb emplace_back
#define all(x) x.begin(),x.end()
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;
const int N=1e6+5,M=N*4+5,K=1000+5,mod=1e9+7,Mod=mod-1;const db eps=1e-9;const int INF=1e9+7;mt19937 rnd(28382);
#define Tp template<typename T>
#define Ts template<typename T,typename... Ar>
namespace Debug{
Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;}
Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
#ifdef LOCAL
#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
#else
#define gdb(...) void()
#endif
}using namespace Debug;
int n,m,k;
vector<pii> S1[N],S2[N],S[N];
vector<tuple<int,int,int>> Q[N];
int in[N],ti[N],ans[N],st[N],sh,g[N];
void addqry(int x,int y,int id,int w){
for(auto [a,b]:S[x]) for(auto [c,d]:S[y]) if(a==c) ans[id]=min(ans[id],w+b+d);
Q[x].emplace_back(y,id,w);Q[y].emplace_back(x,id,w);
}
void Solve(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int x,y,z;scanf("%d%d%d",&x,&y,&z);
S[x].emplace_back(y,z);S[y].emplace_back(x,z);
in[x]++;in[y]++;
}
queue<int> q;
auto check=[&](int x){
if(in[x]<=5&&in[x]>=0) q.push(x),in[x]=-1;
};
for(int i=1;i<=n;i++) check(i);
for(int i=1;i<=n;i++){
int x=q.front();q.pop();ti[x]=i;
for(auto [a,b]:S[x]) in[a]--,check(a);
}
for(int i=1;i<=n;i++){
for(auto [a,b]:S[i]) (ti[i]<ti[a]?S1[i]:S2[i]).emplace_back(a,b);
}
scanf("%d",&k);
for(int i=1;i<=k;i++){
int x,y;scanf("%d%d",&x,&y);
ans[i]=INF;
addqry(x,y,i,0);
for(auto [a,b]:S1[x]) addqry(a,y,i,b);
for(auto [a,b]:S1[y]) addqry(a,x,i,b);
}
Me(g,0x3f);
for(int i=1;i<=n;i++){
st[++sh]=i;g[i]=0;
for(auto [a,b]:S1[i]){
g[a]=min(g[a],b);st[++sh]=a;
for(auto [c,d]:S1[i]) g[c]=min(g[c],b+d),st[++sh]=c;
}
for(auto [x,y,w]:Q[i]) ans[y]=min(ans[y],g[x]+w);
for(auto [a,b]:S2[i]) for(auto [c,d]:S1[a]){
g[c]=min(g[c],b+d);st[++sh]=c;
for(auto [e,f]:S1[c]) g[e]=min(g[e],b+d+f),st[++sh]=e;
}
for(auto [x,y,w]:Q[i]) if(!w) ans[y]=min(ans[y],g[x]+w);
while(sh) g[st[sh--]]=INF;
}
for(int i=1;i<=k;i++) printf("%d\n",ans[i]>=INF?-1:ans[i]);
}
int main(){
int t=1;
// scanf("%d",&t);
while(t--) Solve();
cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 9ms
memory: 111036kb
input:
6 9 1 2 4 2 3 6 3 6 5 6 5 3 5 4 2 4 1 3 3 4 9 1 3 100 5 3 1 5 1 3 1 6 3 4 3 5 2 5
output:
6 8 3 1 7
result:
ok 5 number(s): "6 8 3 1 7"
Test #2:
score: 0
Accepted
time: 15ms
memory: 111112kb
input:
6 4 1 2 1 2 3 1 3 4 1 4 5 1 3 1 4 1 5 1 6
output:
3 -1 -1
result:
ok 3 number(s): "3 -1 -1"
Test #3:
score: 0
Accepted
time: 710ms
memory: 312440kb
input:
40005 79608 1 2 70031203 1 3 99924845 1 4 61645659 1 5 9324967 2 3 15761918 3 4 62534796 4 5 35260314 5 2 35948540 6 2 23727405 6 7 83302920 7 3 31010426 7 8 75060393 8 4 94275932 8 9 99663793 9 5 81701979 9 6 439297 10 6 46955645 10 11 89514237 11 7 21257310 11 12 53896253 12 8 67933315 12 13 26161...
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 1000000 numbers
Test #4:
score: 0
Accepted
time: 703ms
memory: 327832kb
input:
35479 70156 1 2 53094201 1 3 95796673 1 4 35585979 1 5 55612594 2 3 60766083 3 4 64392832 4 5 32896460 5 2 91649893 6 2 6196154 6 7 4986564 7 3 91799790 7 8 10909791 8 4 30034265 8 9 95672010 9 4 67004237 9 10 77872672 10 5 68900058 10 6 42927604 11 6 71288663 11 12 51597962 12 7 79690815 12 13 9742...
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 1000000 numbers
Test #5:
score: 0
Accepted
time: 664ms
memory: 327772kb
input:
35811 70820 1 2 40434193 1 3 13483892 1 4 32864259 1 5 47591755 1 6 65123023 1 7 81695948 1 8 1102880 1 9 47223939 1 10 52947058 1 11 31439481 2 3 94162364 3 4 20590842 4 5 24137043 5 6 74926235 6 7 9376267 7 8 97130364 8 9 75568799 9 10 5022411 10 11 59066963 11 2 96177033 12 2 17823959 12 13 83906...
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 1000000 numbers
Test #6:
score: 0
Accepted
time: 968ms
memory: 251612kb
input:
200000 599952 127401 69434 88680591 127401 39916 10673559 127401 52475 59546013 127401 77787 74018113 127401 11462 7023970 60723 37187 65141305 60723 115008 72307785 60723 71812 47362248 60723 143858 20042617 60723 153890 48502784 60723 172009 21754689 60723 23327 97998405 63817 58332 30056889 63817...
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 1000000 numbers
Test #7:
score: 0
Accepted
time: 956ms
memory: 246256kb
input:
200000 599962 127401 36130 68347938 127401 56107 50001021 127401 35011 47850437 127401 166086 58628679 127401 167575 97121890 127401 150008 97636763 127401 148173 79875436 60723 38275 3780759 60723 75775 69051390 60723 93280 43415633 60723 108525 89666833 60723 119851 80916915 60723 134418 23881201 ...
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 1000000 numbers
Test #8:
score: 0
Accepted
time: 952ms
memory: 247020kb
input:
200000 599957 127401 62215 44179748 127401 96198 78839479 127401 81659 96992357 127401 105920 13674618 127401 192829 54389653 60723 66329 69331527 60723 139474 14904280 60723 186870 86233097 60723 134036 31587848 60723 161658 81495543 60723 156381 83635122 60723 187612 77920441 60723 189714 55041890...
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 1000000 numbers
Test #9:
score: 0
Accepted
time: 662ms
memory: 264936kb
input:
35011 70019 1 2 65752645 2 3 52997572 3 4 14100539 4 5 78562579 5 6 99655664 6 7 26904774 7 8 88411503 8 9 89420520 9 10 88149169 10 11 53207001 11 12 16506048 12 13 37143703 13 14 1295901 14 15 61139259 15 16 79880287 16 17 4390087 17 18 2733598 18 19 48969502 19 20 77562639 20 21 53171540 21 22 36...
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 1000000 numbers
Test #10:
score: 0
Accepted
time: 661ms
memory: 249636kb
input:
50000 99997 1 2 5460409 2 3 95815999 3 4 17091945 4 5 91580749 5 6 91717493 6 7 55420828 7 8 10603109 8 9 62048169 9 10 28446542 10 11 38346952 11 12 66167473 12 13 80749600 13 14 20348320 14 15 5868535 15 16 45627426 16 17 2445360 17 18 21766361 18 19 20516369 19 20 8226530 20 21 86609482 21 22 134...
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 1000000 numbers
Test #11:
score: -100
Time Limit Exceeded
input:
50000 99997 1 2 92917899 2 3 90125226 3 4 10806409 4 5 56452040 5 6 34960749 6 7 14085023 7 8 96350028 8 9 49181182 9 10 70920046 10 11 3727766 11 12 42039112 12 13 63921049 13 14 7634090 14 15 24597384 15 16 13087802 16 17 63136838 17 18 58806049 18 19 98769520 19 20 943620 20 21 70246129 21 22 145...