Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int,int>
- #define vec vector
- using namespace std;
- using ll = long long;
- using ld = long double;
- using db = double;
- void cv(vector <int> &v){
- for (auto x: v) cout<<x+1<<' ';
- cout<<"\n";
- }
- void cvl(vector <ll> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvv(vector <vector <int> > &v){
- for (auto x: v) cv(x);
- cout<<"\n";
- }
- struct ed{
- int fr, to, w;
- };
- const int inf = 2e9;
- int n,m,s,f;
- vec <vec <ed>> G;
- vec <int> d;
- vec <int> pr;
- set <pii> Q;
- vector <bool> was;
- void Dij(int s){
- d.assign(n, inf);
- pr.assign(n, -1);
- Q.insert({0, s});
- was.assign(n, 0);
- d[s] = 0;
- while (!Q.empty()){
- int v = Q.begin() -> second;
- Q.erase(Q.begin());
- //was[v] = 1;
- for (ed e: G[v]){
- //if (was[e.to]) continue;
- if (d[e.to] > d[v] + e.w){
- Q.erase({d[e.to], e.to});
- d[e.to] = d[v] + e.w;
- pr[e.to] = v;
- Q.insert({d[e.to], e.to});
- }
- }
- }
- }
- int main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- cin>>n>>m>>s>>f;
- --s;
- --f;
- G.resize(n);
- for (int i=0;i<m;++i){
- int a,b,w;
- cin>>a>>b>>w;
- a--;
- b--;
- G[a].push_back({a, b, w});
- G[b].push_back({b, a, w});
- }
- Dij(s);
- if (d[f] == inf) {
- cout<<-1;
- exit(0);
- }
- cout<<d[f]<<"\n";
- int v=f;
- vec <int> way ;
- while (v>=0){
- way.push_back(v);
- v = pr[v];
- }
- reverse(way.begin(), way.end());
- //cv(way);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement