Cod sursa(job #2425073)

Utilizator jaocChitu Stefan Catalin jaoc Data 24 mai 2019 10:53:19
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.7 kb
// Ex1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "pch.h"
#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
#include <queue>
#include <fstream>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");


vector<pair<int, pair<int, int> > > E;
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > Q;
vector<int> d;

int main()
{
	int n, m;
	cin >> n >> m;
	d.resize(n + 1);
	for (int i = 0; i < m; i++) {
		int x, y, d;
		cin >> x >> y >> d;
		E.push_back({ d,{x,y} });
	}
	for (int i = 1; i <= n; i++) {
		d[i] = 30000;
	}
	d[1] = 0;
	Q.push({ 0,1 });
	while (!Q.empty()) {
		int u = Q.top().second;
		Q.pop();
		for (auto i = E.begin(); i != E.end(); i++) {
			int x = (*i).second.first;
			int y = (*i).second.second;
			int w = (*i).first;
			if (x == u) {
				if (d[y] > d[x] + w) {
					d[y] = d[x] + w;
					Q.push({ d[y],y });
				}
			}
		}
	}
	for (int i = 2; i <= n; i++)
		out << d[i] << " ";
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file