minimario

Untitled

Jun 24th, 2015
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. #include <utility>
  2. #define pb push_back
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <queue>
  6. #include <vector>
  7.  
  8.  
  9. using namespace std;
  10. long long dist[100005];
  11. int path[100005];
  12. queue<int> q;
  13. vector<int> in_queue(100005, 0);
  14. vector<pair<int, int> > g[100005];
  15. int main() {
  16. ios::sync_with_stdio(0);
  17. cin.tie(0);
  18.  
  19. in_queue[1] = 1;
  20.  
  21. for (long long i=0; i < 100005; i++) {
  22. dist[i] = 900190019001;
  23. }
  24.  
  25. dist[1] = 0;
  26.  
  27.  
  28. int n, m;
  29. cin >> n >> m;
  30. for (int i=0; i < m; i++) {
  31. int a, b, w;
  32. cin >> a >> b >> w;
  33. g[a].pb(pair<int, long long>(b, w));
  34. g[b].pb(pair<int, long long>(a, w));
  35. }
  36.  
  37. q.push(1);
  38.  
  39. while (!q.empty()) {
  40. int u = q.front();
  41. q.pop();
  42. in_queue[u] = 0;
  43. for (int j=0; j < g[u].size(); j++) {
  44. int v = g[u][j].first;
  45. long long w = g[u][j].second;
  46. if (dist[u] + w < dist[v]) {
  47. dist[v] = dist[u] + w;
  48. if (!in_queue[v]) {
  49. q.push(v);
  50. in_queue[v] = 1;
  51. }
  52. }
  53. }
  54. }
  55. if (dist[n] == 900190019001) {
  56. cout << -1 << endl;
  57. return 0;
  58. }
  59.  
  60. vector<int> x;
  61. int i = n;
  62. while (i != 1) {
  63. x.pb(i);
  64. i = path[i];
  65. }
  66. x.pb(1);
  67.  
  68. for (int i = x.size()-1; i >= 0; i--) {
  69. cout << x[i] << " ";
  70. }
  71. return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment