This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub mugen1337/procon
#define PROBLEM "https://judge.yosupo.jp/problem/shortest_path" #include "../template.hpp" #include "../Graph/Dijkstra.hpp" signed main(){ int n,m,s,t;cin>>n>>m>>s>>t; Dijkstra<ll> d(n); rep(i,m){ int u,v;ll c;cin>>u>>v>>c; d.add_edge(u,v,c); } d.build(s); auto p=d.get_path(t); if(p.empty()) cout<<-1<<endl; else{ cout<<d[t]<<" "<<(int)p.size()-1<<endl; int k=p[0]; for(int i=1;i<(int)p.size();i++){ cout<<k<<" "<<p[i]<<endl; k=p[i]; } } return 0; }
#line 1 "test/yosupo_Shortest_Path.test.cpp" #define PROBLEM "https://judge.yosupo.jp/problem/shortest_path" #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_Shortest_Path.test.cpp" #line 1 "Graph/Dijkstra.hpp" template<typename T> struct Dijkstra{ const T TINF=numeric_limits<T>::max(); using P=pair<T,int>; int n; vector<vector<P>> G; vector<T> d; vector<int> prev; Dijkstra(int n):n(n),G(vector<vector<P>>(n)){} void init(){ d.assign(n,TINF); prev.assign(n,-1); } void add_edge(int s,int t,T cost){ G[s].push_back(P(cost,t)); } void build(int s){ init(); d[s]=0; priority_queue<P,vector<P>,greater<P>> q; q.push(P(d[s],s)); while(!q.empty()){ P p=q.top();q.pop(); int v=p.second; if(d[v]<p.first) continue; for(auto e:G[v]){ int u=e.second; T cost=e.first; if(d[u]>d[v]+e.first){ d[u]=d[v]+cost; prev[u]=v; q.push(P(d[u],u)); } } } } T operator[](const int &p)const{ return d[p]; } vector<int> get_path(int g){ vector<int> ret; if(d[g]==TINF) return ret; for(;g!=-1;g=prev[g]){ ret.push_back(g); } reverse(ret.begin(),ret.end()); return ret; } }; #line 6 "test/yosupo_Shortest_Path.test.cpp" signed main(){ int n,m,s,t;cin>>n>>m>>s>>t; Dijkstra<ll> d(n); rep(i,m){ int u,v;ll c;cin>>u>>v>>c; d.add_edge(u,v,c); } d.build(s); auto p=d.get_path(t); if(p.empty()) cout<<-1<<endl; else{ cout<<d[t]<<" "<<(int)p.size()-1<<endl; int k=p[0]; for(int i=1;i<(int)p.size();i++){ cout<<k<<" "<<p[i]<<endl; k=p[i]; } } return 0; }