Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <queue>
  4.  
  5. using namespace std;
  6.  
  7. typedef pair<uint32_t, uint64_t> Edge;
  8. bool operator < (const Edge &l, const Edge &r) {
  9.     return l.second > r.second;
  10. }
  11.  
  12. int main() {
  13.     ifstream fin("input.txt");
  14.     ofstream fout("output.txt");
  15.  
  16.     size_t vertexN, edgeN;
  17.     fin >> vertexN >> edgeN;
  18.     vector<uint64_t> dist(vertexN, UINT64_MAX);
  19.     vector<vector<Edge>> edges(vertexN);
  20.  
  21.     for (uint32_t i = 0, a, b, weight; i < edgeN; ++i) {
  22.         fin >> a >> b >> weight;
  23.         --a; --b;
  24.         edges[a].push_back(Edge(b, weight));
  25.         edges[b].push_back(Edge(a, weight));
  26.     }
  27.  
  28.     dist[0] = 0;
  29.     priority_queue<Edge> minDistance;
  30.     minDistance.push(make_pair(0, 0));
  31.  
  32.     while (!minDistance.empty()) {
  33.         Edge current = minDistance.top();
  34.         minDistance.pop();
  35.         if (current.second <= dist[current.first]) {
  36.             for (Edge next : edges[current.first]) {
  37.                 if (dist[next.first] > next.second + current.second) {
  38.                     dist[next.first] = next.second + current.second;
  39.                     minDistance.push(make_pair(next.first, dist[next.first]));
  40.                 }
  41.             }
  42.         }
  43.     }
  44.  
  45.     fout << dist[vertexN - 1];
  46.  
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement