QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#324231 | #8232. Yet Another Shortest Path Query | ucup-team266# | WA | 3460ms | 260552kb | C++23 | 2.8kb | 2024-02-10 17:10:49 | 2024-02-10 17:10:51 |
Judging History
answer
//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
void die(string S){puts(S.c_str());exit(0);}
int B=1000;
int n,m,q;
int u[1001000],v[1001000],w[1001000];
vector<pii> G[1001000];
int deg[1001000];
int a[1001000],b[1001000];
int ans[1001000];
bitset<1024> bs[1000001];
map<pii,int> Mp;
vector<int> large;
vector<pii> vq[1001000];
int dist[1001000];
int memo[1001000];
map<pii,int> Mem;
int calc(int x,int y)
{
int ans=inf;
for(auto pr:G[y])
{
int p=lb(G[x],mp(pr.first,0));
if(p<sz(G[x])&&G[x][p].first==pr.first)
ans=min(ans,G[x][p].second+pr.second);
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
B=2*sqrt(n);
for(int i=1;i<=m;i++)
{
cin>>u[i]>>v[i]>>w[i];
G[u[i]].pb(v[i],w[i]);
G[v[i]].pb(u[i],w[i]);
deg[u[i]]++;
deg[v[i]]++;
Mp[mp(u[i],v[i])]=w[i];
Mp[mp(v[i],u[i])]=w[i];
}
for(int i=1;i<=n;i++)
srt(G[i]);
cin>>q;
for(int i=1;i<=q;i++)
{
cin>>a[i]>>b[i];
if(Mem.count(mp(a[i],b[i])))
{
memo[i]=Mem[mp(a[i],b[i])];
continue;
}
Mem[mp(a[i],b[i])]=Mem[mp(b[i],a[i])]=i;
}
memset(ans,0x3f,sizeof(ans));
for(int i=1;i<=q;i++)
if(Mp.count(mp(a[i],b[i])))
ans[i]=Mp[mp(a[i],b[i])];
for(int i=1;i<=n;i++)
if(deg[i]>B)
large.pb(i);
srt(large);
assert(sz(large)<=1024);
for(int i=1;i<=m;i++)
{
if(deg[u[i]]>B)
bs[v[i]][lb(large,u[i])]=1;
if(deg[v[i]]>B)
bs[u[i]][lb(large,v[i])]=1;
}
for(int i=1;i<=q;i++) if(!memo[i])
{
if(deg[a[i]]>B)
vq[a[i]].pb(b[i],i);
else if(deg[b[i]]>B)
vq[b[i]].pb(a[i],i);
else
{
ans[i]=min(ans[i],calc(a[i],b[i]));
for(auto pr:G[a[i]])
ans[i]=min(ans[i],calc(pr.first,b[i])+pr.second);
}
}
for(int i=1;i<=n;i++)
if(deg[i]>B)
{
memset(dist,0x3f,sizeof(dist));
dist[i]=0;
for(int j=0;j<3;j++)
for(int k=1;k<=m;k++)
{
dist[v[k]]=min(dist[v[k]],dist[u[k]]+w[k]);
dist[u[k]]=min(dist[u[k]],dist[v[k]]+w[k]);
}
for(auto pr:vq[i])
ans[pr.second]=min(ans[pr.second],dist[pr.first]);
}
for(int i=1;i<=q;i++)
{
if(memo[i])
ans[i]=ans[memo[i]];
if(ans[i]<inf)
cout<<ans[i]<<'\n';
else
cout<<"-1\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 27188kb
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: 2ms
memory: 20012kb
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: 1055ms
memory: 73976kb
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: 1068ms
memory: 71468kb
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: 1145ms
memory: 75432kb
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: 3434ms
memory: 260552kb
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: 3460ms
memory: 258832kb
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: 3459ms
memory: 260180kb
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: 1457ms
memory: 132708kb
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: 1649ms
memory: 161020kb
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
Wrong Answer
time: 1373ms
memory: 172164kb
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...
output:
53806372 101314854 124850165 38514625 96220435 122794730 84721400 112668433 107849606 160932332 116266392 101162529 140732651 93988737 87814230 71121316 72361729 54614389 95539309 119384111 95776872 109513557 60646451 142757728 61377855 40164780 49674827 71836734 15251469 135919057 66944724 83240312...
result:
wrong answer 16107th numbers differ - expected: '58584789', found: '44452628'