QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#882556 | #8232. Yet Another Shortest Path Query | DaiRuiChen007 | WA | 2911ms | 316780kb | C++17 | 2.0kb | 2025-02-05 09:21:57 | 2025-02-05 09:22:05 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#include<ext/pb_ds/hash_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
using namespace std;
const int MAXN=1e6+5,inf=1e9;
struct Edge { int v,w; };
inline void chkmin(int &x,const int &y) { x=y<x?y:x; }
int n,m,q,ans[MAXN],dg[MAXN],rk[MAXN];
vector <Edge> G[MAXN],rE[MAXN],E[MAXN];
vector <array<int,2>> Q[MAXN];
__gnu_pbds::gp_hash_table <ll,int> D;
int qry(int u,int v) {
if(u==v) return 0;
ll p=1ll*min(u,v)*MAXN+max(u,v);
int s=(D.find(p)!=D.end()?D[p]:inf);
if(rk[u]>rk[v]) swap(u,v);
for(auto e1:E[u]) {
if(v==e1.v) chkmin(s,e1.w);
for(auto e2:E[e1.v]) if(e2.v==v) chkmin(s,e1.w+e2.w);
for(auto e2:E[v]) if(e1.v==e2.v) chkmin(s,e1.w+e2.w);
}
return s;
}
int stk[MAXN],tp,f[MAXN];
signed main() {
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n>>m;
for(int i=1,u,v,w;i<=m;++i) cin>>u>>v>>w,G[u].push_back({v,w}),G[v].push_back({u,w});
queue <int> qu;
for(int i=1;i<=n;++i) if((dg[i]=G[i].size())<6) qu.push(i),dg[i]=-1;
for(int i=1;i<=n;++i) {
int u=qu.front(); rk[u]=i,qu.pop();
for(auto e:G[u]) if(~dg[e.v]&&(--dg[e.v]<6)) qu.push(e.v);
}
for(int u=1;u<=n;++u) for(auto e:G[u]) (rk[e.v]>rk[u]?E:rE)[u].push_back(e);
for(int u=1;u<=n;++u) for(auto e1:G[u]) for(auto e2:G[u]) if(e1.v<e2.v) {
ll p=1ll*e1.v*MAXN+e2.v;
if(D.find(p)==D.end()) D[p]=e1.w+e2.w;
else chkmin(D[p],e1.w+e2.w);
}
cin>>q;
for(int i=1,u,v;i<=q;++i) {
cin>>u>>v,ans[i]=inf;
chkmin(ans[i],qry(u,v));
for(auto e:E[u]) chkmin(ans[i],e.w+qry(e.v,v));
for(auto e:E[v]) chkmin(ans[i],e.w+qry(u,e.v));
Q[u].push_back({v,i}),Q[v].push_back({u,i});
}
fill(f+1,f+n+1,inf);
for(int u=1;u<=n;++u) {
for(auto e1:rE[u]) for(auto e2:E[e1.v]) for(auto e3:E[e2.v]) {
if(f[e3.v]==inf) stk[++tp]=e3.v;
chkmin(f[e3.v],e1.w+e2.w+e3.w);
}
for(auto o:Q[u]) chkmin(ans[o[1]],f[o[0]]);
while(tp) f[stk[tp--]]=inf;
}
for(int i=1;i<=q;++i) cout<<(ans[i]==inf?-1:ans[i])<<"\n";
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 17ms
memory: 104168kb
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: 12ms
memory: 104040kb
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: 419ms
memory: 153464kb
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: 439ms
memory: 153212kb
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: 452ms
memory: 153592kb
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: -100
Wrong Answer
time: 2911ms
memory: 316780kb
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:
wrong answer 10010th numbers differ - expected: '102496890', found: '147880267'