QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#672032#2879. Kaleidoscopic RouteFolity#TL 1ms5884kbC++202.2kb2024-10-24 15:24:282024-10-24 15:24:28

Judging History

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

  • [2024-10-24 15:24:28]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:5884kb
  • [2024-10-24 15:24:28]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

const int N=1e5+5,M=2e5+5;
int n,m,x[M],y[M],z[M];
vector<pii> e[N];
int diss[N],dist[N],d[N];
void adde(int x,int y,int z){
    e[x].emplace_back(y,z);
}
int f[N],g[N],pf[N],pg[N],nu,nv;
bool fl;
int pre[N];
bool vis[N];

int main(){
    cin>>n>>m;
    for(int i=1;i<=m;++i){
        cin>>x[i]>>y[i]>>z[i];
        adde(x[i],y[i],z[i]),adde(y[i],x[i],z[i]);
    }
    queue<int> q;
    q.push(1);
    diss[1]=1;
    while(!q.empty()){
        int u=q.front();
        q.pop();
        for(auto &[v,w]:e[u]){
            if(!diss[v])diss[v]=diss[u]+1,q.push(v);
        }
    }
    q.push(n);
    dist[n]=1;
    while(!q.empty()){
        int u=q.front();
        q.pop();
        for(auto &[v,w]:e[u]){
            if(!dist[v])dist[v]=dist[u]+1,q.push(v);
        }
    }
    cout<<diss[n]-1<<'\n';
    for(int i=1;i<=n;++i)e[i].clear();
    for(int i=1;i<=m;++i){
        if(diss[x[i]]+dist[y[i]]==diss[n])adde(x[i],y[i],z[i]),++d[y[i]];
        if(diss[y[i]]+dist[x[i]]==diss[n])adde(y[i],x[i],z[i]),++d[x[i]];
    }
    for(int i=2;i<=n;++i)f[i]=1e9,g[i]=-1e9;
    for(auto &[v,w]:e[1])f[v]=g[v]=w,pf[v]=pg[v]=1,q.push(v),--d[v];
    int ans=0;
    while(!q.empty()){
        int u=q.front();
        q.pop();
        for(auto &[v,w]:e[u]){
            if(w-f[u]>ans)ans=w-f[u],nu=u,nv=v,fl=0;
            if(g[u]-w>ans)ans=g[u]-w,nu=u,nv=v,fl=1;
            if(min(f[u],w)<f[v])f[v]=min(f[u],w),pf[v]=u;
            if(max(g[u],w)>g[v])g[v]=max(g[u],w),pg[v]=u;
            if(!--d[v])q.push(v);
        }
    }
    vector<int> ansl,ansr;
    ansl.push_back(nu);
    if(!fl){
        while(nu!=1)nu=pf[nu],ansl.push_back(nu);
    }
    else{
        while(nu!=1)nu=pg[nu],ansl.push_back(nu);
    }
    ansr.push_back(n);
    q.push(nv);
    while(!q.empty()){
        int u=q.front();
        q.pop();
        for(auto &[v,w]:e[u])if(!vis[v])q.push(v),pre[v]=u;
    }
    int nw=n;
    while(nw!=nv)nw=pre[nw],ansr.push_back(nw);
    for(int i=ansl.size()-1;i>=0;--i)cout<<ansl[i]<<' ';
    for(int i=ansr.size()-1;i>=0;--i)cout<<ansr[i]<<' ';
    cout<<'\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5884kb

input:

6 8
1 5 2
5 2 5
3 5 4
1 3 10
3 4 6
4 5 7
4 6 8
2 6 1

output:

3
1 5 4 6 

result:

ok 3 6

Test #2:

score: -100
Time Limit Exceeded

input:

10 20
1 9 34
6 3 80
7 3 15
5 4 73
4 1 42
8 6 92
2 10 25
4 3 8
9 3 79
3 1 11
9 2 74
10 1 83
3 2 6
10 3 38
10 4 48
1 5 38
6 2 43
4 2 55
8 7 90
3 5 4

output:


result: