Advertisement
zholnin

Untitled

May 18th, 2014
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include <functional>
  2. #include <numeric>
  3. #include <utility>
  4. #include <sstream>
  5. #include <iostream>
  6. #include <iomanip>
  7. #include <cstdio>
  8. #include <cmath>
  9. #include <cstdlib>
  10. #include <ctime>
  11. #include <iostream>
  12. #include <map>
  13. #include <vector>
  14. #include <queue>
  15.  
  16. using namespace std;
  17.  
  18.  
  19. int main()
  20. {
  21.     ios_base::sync_with_stdio(0);
  22.  
  23.     int n, m;
  24.  
  25.     cin >> n >> m;
  26.  
  27.     vector<vector<pair<int, int>>> G(n + 1);
  28.     vector<int> visited(n+1);
  29.  
  30.     int a, b, c;
  31.  
  32.     for (int i = 0; i < m; i++)
  33.     {
  34.         cin >> a >> b >> c;
  35.         G[a].push_back(pair<int, int>(b, c));
  36.         G[b].push_back(pair<int, int>(a, c));
  37.     };
  38.  
  39.     priority_queue<vector<int>> Queue;
  40.     vector<int> entry(3); // -distance, nRoads, Node
  41.     entry[0] = 0;
  42.     entry[1] = 0;
  43.     entry[2] = 1;
  44.  
  45.     Queue.push(entry);
  46.  
  47.     int vertex, numroads;
  48.     int length;
  49.  
  50.     while (Queue.size())
  51.     {
  52.         entry = Queue.top();  Queue.pop();
  53.         vertex = entry[2]; length = -entry[0]; numroads = entry[1];
  54.  
  55.         if (vertex == n)
  56.             break;
  57.  
  58.         if (visited[vertex]) continue;
  59.         visited[vertex] = 1;
  60.  
  61.         for (size_t i = 0; i < G[vertex].size(); i++)
  62.         {
  63.             if (!visited[G[vertex][i].first])
  64.             {
  65.                 entry[2] = G[vertex][i].first;
  66.                 entry[0] = -(length + G[vertex][i].second);
  67.                 entry[1] = numroads + 1;
  68.                 Queue.push(entry);
  69.             }
  70.         }
  71.     }
  72.  
  73.     cout << length << " " << numroads << endl;
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement