Cod sursa(job #3257109)

Utilizator Tudor.1234Holota Tudor Matei Tudor.1234 Data 16 noiembrie 2024 17:56:51
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include "bits/stdc++.h"
const int SIZE = 1000;
std :: vector < std :: vector < std :: pair < int, int > > > adj; // adj[i].first = node, adj[i].second  = cost
// min-heap : {cost, node}
std :: priority_queue < std :: pair < int, int >,  std :: vector < std :: pair < int, int > >, std :: greater <> > pq;
int n, m;
std :: vector < int > dist;
void Dijkstra(int start){
	dist[start] = 0;
	pq.push({0, start});
	while(!pq.empty()){
		auto [cost, node]  = pq.top();
		pq.pop();
		if(cost > dist[node]){
			continue;
		}
		for(auto vecin : adj[node]){
			if(dist[node] + vecin.second < dist[vecin.first]){
				dist[vecin.first] = dist[node] + vecin.second;
				pq.push({dist[node] + vecin.second, vecin.first});
			}
		}
	}
}
void Solve(){
	std :: cin >> n >> m;
	adj.resize(n + 1);
	while(m -- ){
		int x, y, cost;
		std :: cin  >> x >> y >> cost;
		adj[x].push_back({y, cost});
	}
	dist.resize(n + 1, INT_MAX);
	Dijkstra(1);
	for(int i = 2; i <= n; i++){
		if(dist[i] != INT_MAX){
			std :: cout << dist[i] << ' ';
		}
		else{
			std :: cout << 0 << ' ';
		}
	}
}
signed main(){
	freopen("dijkstra.in", "r", stdin);
	freopen("dijkstra.out", "w", stdout);
	std :: ios_base :: sync_with_stdio(false);
	std :: cin.tie(0);
	std :: cout.tie(0);
	Solve();
	return 0;
}