QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#669310#8232. Yet Another Shortest Path QueryqzezWA 11560ms43640kbC++141.8kb2024-10-23 18:06:362024-10-23 18:06:36

Judging History

你现在查看的是最新测评结果

  • [2024-10-23 18:06:36]
  • 评测
  • 测评结果:WA
  • 用时:11560ms
  • 内存:43640kb
  • [2024-10-23 18:06:36]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10,INF=1e9;
int n,m,q;
vector<tuple<int,int,int>>E;
mt19937 rnd(time(0));
vector<tuple<int,int,int>>Q;
int p[N];
int find(int x){
    return p[x]==x?x:p[x]=find(p[x]);
}
struct edges{
    int to,w,nex;
}edge[N*2];
int kk,head[N];
void add(int u,int v,int w){
    edge[++kk]={v,w,head[u]},head[u]=kk;
}
int vis[N],fa[N];
int dis[N];
void dfs(int u,int fa=0){
    vis[u]=1;
    ::fa[u]=fa;
    for(int i=head[u];i;i=edge[i].nex){
        int v=edge[i].to;
        if(v==fa)continue;
        dis[v]=dis[u]+edge[i].w;
        dfs(v,u);
    }
}
void run(){
    shuffle(E.begin(),E.end(),rnd);
    iota(p,p+1+n,0);
    kk=0,fill(head,head+1+n,0);
    for(auto [u,v,w]:E){
        int x=find(u),y=find(v);
        if(x==y)continue;
        add(u,v,w),add(v,u,w);
        p[x]=y;
    }
    fill(vis,vis+1+n,0);
    for(int i=1;i<=n;i++)if(!vis[i])dis[i]=0,dfs(i);
    for(auto &[u,v,ans]:Q){
        if(dis[u]>dis[v])swap(u,v);
        if(fa[v]==u)ans=min(ans,dis[v]-dis[u]);
        else if(fa[v]==fa[u]&&fa[u])ans=min(ans,dis[u]+dis[v]-dis[fa[u]]*2);
        else if(fa[fa[v]]==u)ans=min(ans,dis[v]-dis[u]);
        else if(fa[fa[fa[v]]]==u)ans=min(ans,dis[v]-dis[u]);
        else if(fa[fa[v]]==fa[u]&&fa[u])ans=min(ans,dis[v]+dis[u]-dis[fa[u]]*2);
        else if(fa[v]&&fa[v]==fa[fa[u]])ans=min(ans,dis[v]+dis[u]-dis[fa[v]]*2);
    }
}
int main(){
    scanf("%d%d",&n,&m);
    E.resize(m);
    for(auto &[u,v,w]:E){
        scanf("%d%d%d",&u,&v,&w);
    }
    scanf("%d",&q);
    Q.resize(q);
    for(auto &[u,v,ans]:Q){
        scanf("%d%d",&u,&v);
        ans=INF;
    }
    for(;;){
        run();
        if(1.0*clock()/CLOCKS_PER_SEC>11.5)break;
    }
    for(auto [u,v,ans]:Q)printf("%d\n",ans<INF?ans:-1);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 8915ms
memory: 14120kb

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: 7009ms
memory: 14188kb

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: 11555ms
memory: 29428kb

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: 11558ms
memory: 30364kb

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: 11560ms
memory: 26248kb

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: 11524ms
memory: 43640kb

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 13460th numbers differ - expected: '26964181', found: '58377675'