Endil

Untitled

Nov 9th, 2015
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <string>
  7. #include <cmath>
  8. #include <set>
  9. #include <queue>
  10. #include <map>
  11. #include <climits>
  12.  
  13. using namespace std;
  14.  
  15. typedef long long int ll;
  16. typedef double de;
  17.  
  18. const ll INF = LLONG_MAX;
  19.  
  20. #define mp(x,y) pair<int,int>(x,y)
  21.  
  22. int main()
  23. {
  24.         ios_base::sync_with_stdio(false);
  25.         ifstream cin("distance.in");
  26.         ofstream cout("distance.out");
  27.         int n,m,f,s;
  28.         cin >> n >> m >> s >> f;s--,f--;
  29.         vector<vector<pair<int,int> > > g(n);
  30.         vector<ll> d(n,INF),p(n);
  31.         for(int i = 0;i < m;i++)
  32.         {
  33.                 int a,b,c;
  34.                 cin >> a >> b >> c;a--,b--;
  35.                 g[a].push_back(mp(b,c));
  36.                 g[b].push_back(mp(a,c));
  37.         }
  38.         priority_queue<pair<int,int> > q;
  39.         q.push(mp(0,s));
  40.         d[s] = 0;
  41.         while(!q.empty())
  42.         {
  43.                 int v = q.top().second, dis = -q.top().first;
  44.                 q.pop();
  45.                 if(d[v] < dis) continue;
  46.                 for(int i = 0;i < g[v].size();i++)
  47.                 {
  48.                         int to = g[v][i].first,len = g[v][i].second;
  49.                         if(d[to] > d[v] + len)
  50.                         {
  51.                                 d[to] = d[v] + len;
  52.                                 p[to] = v;
  53.                                 q.push(mp(-d[to],to));
  54.                         }
  55.                 }
  56.         }
  57.         if(d[f] == INF) {cout << -1 << endl;return 0;}
  58.         vector<int> path;
  59.         for(int i = f;i != s;i = p[i])
  60.                 path.push_back(i);
  61.         path.push_back(s);
  62.         cout << d[f] << endl;
  63.         for(int i = path.size()-1;i >= 0;i--)
  64.                 cout << path[i]+1 << " ";
  65.         cout << endl;
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment