Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <functional>
- #include <numeric>
- #include <utility>
- #include <sstream>
- #include <iostream>
- #include <iomanip>
- #include <cstdio>
- #include <cmath>
- #include <cstdlib>
- #include <ctime>
- #include <iostream>
- #include <map>
- #include <vector>
- #include <queue>
- using namespace std;
- int main()
- {
- ios_base::sync_with_stdio(0);
- int n, m;
- cin >> n >> m;
- vector<vector<pair<int, int>>> G(n + 1);
- vector<int> visited(n+1);
- int a, b, c;
- for (int i = 0; i < m; i++)
- {
- cin >> a >> b >> c;
- G[a].push_back(pair<int, int>(b, c));
- G[b].push_back(pair<int, int>(a, c));
- };
- priority_queue<vector<int>> Queue;
- vector<int> entry(3); // -distance, nRoads, Node
- entry[0] = 0;
- entry[1] = 0;
- entry[2] = 1;
- Queue.push(entry);
- int vertex, numroads;
- int length;
- while (Queue.size())
- {
- entry = Queue.top(); Queue.pop();
- vertex = entry[2]; length = -entry[0]; numroads = entry[1];
- if (vertex == n)
- break;
- if (visited[vertex]) continue;
- visited[vertex] = 1;
- for (size_t i = 0; i < G[vertex].size(); i++)
- {
- if (!visited[G[vertex][i].first])
- {
- entry[2] = G[vertex][i].first;
- entry[0] = -(length + G[vertex][i].second);
- entry[1] = numroads + 1;
- Queue.push(entry);
- }
- }
- }
- cout << length << " " << numroads << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement