Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <vector>
- #include <queue>
- #include <climits>
- using namespace std;
- ifstream inf("dijkstra2.in");
- ofstream outf("dijkstra2.out");
- priority_queue< pair<int, int>, vector< pair<int, int> >, greater< pair<int, int> > > pq;
- vector< pair<int, int> > ad[100001];
- int dist[100001];
- void Dijkstra(int start) {
- pq.push(make_pair(0, start));
- dist[start] = 0;
- while(!pq.empty()) {
- int x = pq.top().second;
- pq.pop();
- for(auto& el : ad[x]) {
- if(dist[el.second] > dist[x] + el.first) {
- dist[el.second] = dist[x] + el.first;
- pq.push(make_pair(dist[el.second], el.second));
- }
- }
- }
- }
- int main() {
- int n, p, x, y, c, m;
- inf >> n >> m >> p;
- while(m) {
- inf >> x >> y >> c;
- ad[x].push_back(make_pair(c, y));
- ad[y].push_back(make_pair(c, x));
- m--;
- }
- for(int i = 1; i <= n; i++) {
- dist[i] = INT_MAX;
- }
- Dijkstra(p);
- for(int i = 1; i <= n; i++) {
- if(dist[i] != INT_MAX) {
- outf << dist[i] << ' ';
- }
- else {
- outf << "-1 ";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement