procon

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub mugen1337/procon

:heavy_check_mark: test/yosupo_KSW.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/k_shortest_walk"

#include "../template.hpp"

#include "../Graph2/Eppstein.hpp"

signed main(){
    int n,m,s,t,k;cin>>n>>m>>s>>t>>k;
    Graph<ll> g(n);
    g.read(m,0,true,true);
    auto res=Eppstein(g,s,t,k);
    rep(i,k){
        if(i<(int)res.size())   cout<<res[i]<<endl;
        else                    cout<<-1<<endl;
    }
    return 0;
}
#line 1 "test/yosupo_KSW.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/k_shortest_walk"

#line 1 "template.hpp"
#include<bits/stdc++.h>
using namespace std;
#define ALL(x) begin(x),end(x)
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 1000000007
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;
 
template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}
template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}

#line 4 "test/yosupo_KSW.test.cpp"

#line 1 "Graph2/GraphTemplate.hpp"



// graph template
// ref : https://ei1333.github.io/library/graph/graph-template.hpp
template<typename T=int>
struct Edge{
    int from,to;
    T w;
    int idx;
    Edge()=default;
    Edge(int from,int to,T w=1,int idx=-1):from(from),to(to),w(w),idx(idx){}
    operator int() const{return to;}
};

template<typename T=int>
struct Graph{
    vector<vector<Edge<T>>> g;
    int V,E;
    Graph()=default;
    Graph(int n):g(n),V(n),E(0){}

    int size(){
        return (int)g.size();
    }
    void resize(int k){
        g.resize(k);
        V=k;
    }
    inline const vector<Edge<T>> &operator[](int k)const{
        return (g.at(k));
    }
    inline vector<Edge<T>> &operator[](int k){
        return (g.at(k));
    }
    void add_directed_edge(int from,int to,T cost=1){
        g[from].emplace_back(from,to,cost,E++);
    }
    void add_edge(int from,int to,T cost=1){
        g[from].emplace_back(from,to,cost,E);
        g[to].emplace_back(to,from,cost,E++);
    }
    void read(int m,int pad=-1,bool weighted=false,bool directed=false){
        for(int i=0;i<m;i++){
            int u,v;cin>>u>>v;
            u+=pad,v+=pad;
            T w=T(1);
            if(weighted) cin>>w;
            if(directed) add_directed_edge(u,v,w);
            else         add_edge(u,v,w);
        }
    }
};


#line 2 "Graph2/Eppstein.hpp"

template<typename T>
struct PersistentLeftistHeapNode{
    PersistentLeftistHeapNode *l,*r;
    int s;
    T val;
    PersistentLeftistHeapNode(T val):l(nullptr),r(nullptr),s(1),val(val){}
};

template<typename T,bool less=true>
struct PersistentLeftistHeap{
    PersistentLeftistHeapNode<T> *root;
    PersistentLeftistHeap(PersistentLeftistHeapNode<T> *t=nullptr):root(t){}

    PersistentLeftistHeapNode<T> *meld(PersistentLeftistHeapNode<T> *a,PersistentLeftistHeapNode<T> *b){
        if(!a or !b) return (a?a:b);
        if((a->val>b->val)^(!less)) swap(a,b);
        a=new PersistentLeftistHeapNode(*a);
        a->r=meld(a->r,b);
        if(!a->l or a->l->s<a->r->s) swap(a->l,a->r);
        a->s=(a->r?a->r->s:0)+1;
        return a;
    }
    PersistentLeftistHeap meld(PersistentLeftistHeap b){
        return PersistentLeftistHeap(meld(root,b.root));
    }
    PersistentLeftistHeap push(T x){
        return PersistentLeftistHeap(meld(root,new PersistentLeftistHeapNode(x)));
    }
    PersistentLeftistHeap pop(){
        assert(root!=nullptr);
        return PersistentLeftistHeap(meld(root->l,root->r));
    }
    bool empty(){
        return (root==nullptr);
    }
    T top(){
        assert(root!=nullptr);
        return root->val;
    }
};

// ref: https://qiita.com/hotman78/items/42534a01c4bd05ed5e1e
// K Shortest Walk
template<typename T>
vector<T> Eppstein(Graph<T> &G,int s,int t,int k){
    int N=G.V,M=G.E;
    T inf=numeric_limits<T>::max();

    Graph<T> rG(N);
    vector<Edge<T>> edges(M);
    for(int i=0;i<N;i++)for(auto &e:G[i]) edges[e.idx]=e;
    for(auto &e:edges) rG.add_directed_edge(e.to,e.from,e.w);

    // Dijkstra rG, make Tree
    vector<int> prev_e(N,-1),prev_v(N,-1);
    vector<T> dis(N,inf);
    vector<vector<int>> tree(N);
    vector<bool> tree_edge(M,false);
    {
        using P=pair<T,int>;
        priority_queue<P,vector<P>,greater<P>> que;
        dis[t]=0;
        que.emplace(dis[t],t);
        while(!que.empty()){
            auto [d_cur,cur]=que.top();que.pop();
            if(dis[cur]<d_cur) continue;
            for(auto &e:rG[cur])if(chmin(dis[e.to],d_cur+e.w)){
                prev_e[e.to]=e.idx;
                prev_v[e.to]=cur;
                que.emplace(dis[e.to],e.to);
            }
        }

        if(dis[s]>=inf) return {};
        for(auto &i:prev_e)if(i>=0) tree[edges[i].to].push_back(edges[i].from),tree_edge[i]=true;
    }

    // make H_G
    vector<PersistentLeftistHeap<pair<T,int>>> H_G(N);// (potential, edge index)
    {
        function<void(int)> dfs=[&](int cur){
            if(prev_v[cur]>=0) H_G[cur]=H_G[cur].meld(H_G[prev_v[cur]]);
            for(auto &e:G[cur]){
                if(e.to!=t and prev_v[e.to]<0) continue; // cant reach
                if(tree_edge[e.idx]) continue;
                H_G[cur]=H_G[cur].push({e.w-dis[cur]+dis[e.to],e.idx});
            }
            for(auto &to:tree[cur]) dfs(to);
        };
        dfs(t);
    }
    

    // return KSP
    vector<T> ret;
    {
        using P_TN=pair<T,PersistentLeftistHeapNode<pair<T,int>>*>;
        auto comp=[](const P_TN &x,const P_TN &y){return x.first>y.first;};
        priority_queue<P_TN,vector<P_TN>,decltype(comp)> que(comp);
        ret.push_back(dis[s]);
        if(H_G[s].root) que.emplace(dis[s]+H_G[s].root->val.first,H_G[s].root);
        while(!que.empty() and (int)ret.size()<k){
            auto [cost,cur]=que.top();que.pop();
            ret.emplace_back(cost);

            int to=edges[cur->val.second].to;
            if(H_G[to].root) que.emplace(cost+H_G[to].root->val.first,H_G[to].root);

            if(cur->l) que.emplace(cost+cur->l->val.first-cur->val.first,cur->l);
            if(cur->r) que.emplace(cost+cur->r->val.first-cur->val.first,cur->r);
        }
    }
    return ret;
}
#line 6 "test/yosupo_KSW.test.cpp"

signed main(){
    int n,m,s,t,k;cin>>n>>m>>s>>t>>k;
    Graph<ll> g(n);
    g.read(m,0,true,true);
    auto res=Eppstein(g,s,t,k);
    rep(i,k){
        if(i<(int)res.size())   cout<<res[i]<<endl;
        else                    cout<<-1<<endl;
    }
    return 0;
}
Back to top page