Cod sursa(job #2041932)

Utilizator Spiromanii_MessiUNIBUCThrowTerror Spiromanii_Messi Data 17 octombrie 2017 21:26:09
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include <bits/stdc++.h>

using namespace std ;

int main(int argc, char const *argv[])
{
	ifstream fin ("dijkstra.in") ;
	ofstream fout ("dijkstra.out") ;
	int n, m; 
	fin >> n >> m ; 
	vector < vector <pair <int, int> > > gr (n + 1) ; 
	while (m --) {
		int x, y, cost ; 
		fin >> x >> y >> cost ; 
		gr [x].push_back ({y, cost}) ; 
	}
	auto comp = [](pair <int, int> A, pair <int, int> B) {
		return A.second > B.second ; 
	};
	priority_queue < pair <int, int>, vector < pair <int, int> >, decltype (comp)> H (comp) ; 
	vector <int> dist (n + 1, 1<< 29) ; 
	dist [1] = 0 ; 
	H.push({1, 0}) ; 
	while (!H.empty()) {
		auto cur = H.top () ;
		H.pop () ; 
		auto nod = cur.first ; 
		auto cost = cur.second ; 
		if (dist [nod] != cost) continue ; 
		for (auto &x : gr [nod]) {
			if (dist [x.first] > dist [nod] + x.second) {
				dist [x.first] = dist [nod] + x.second ; 
				H.push ({x.first, dist [x.first]}) ;
			}
		}
	}
	for (int i = 2 ; i <= n ; ++ i) {
		fout << ((dist [i] >= (1 << 29)) ? 0 : dist [i]) << ' ' ;
	}
	return 0;
}