Pagini recente » Cod sursa (job #700276) | Cod sursa (job #3288357) | Cod sursa (job #2123215) | Cod sursa (job #2656456) | Cod sursa (job #3182301)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int NMAX = 50001;
const long long INF = 5000000000;
vector < pair < int, int > > G[NMAX];
long long dist[NMAX];
int N, M;
void read(){
fin >> N >> M;
for(int i = 0; i < M; i++){
int x, y, c;
fin >> x >> y >> c;
G[x].push_back({y, c});
}
}
void init(){
for(int i = 1; i <= N; i++){
dist[i] = INF;
}
dist[1] = 0;
}
bool relax(int x, int y, int c){
if(dist[y] > dist[x] + c){
dist[y] = dist[x] + c;
}
}
queue < int > q;
int viz[NMAX];
void bellman_ford_optimizat(){
q.push(1);
while(!q.empty()){
int nodc = q.front();
q.pop();
for(auto [nbr, cost]: G[nodc]){
if(dist[nbr] > dist[nodc] + cost){
dist[nbr] = dist[nodc] + cost;
viz[nbr]++;
q.push(nbr);
}
}
}
}
int main()
{
read();
init();
bellman_ford_optimizat();
for(int i = 2; i <= N; i++){
if(dist[i] == INF){
fout << 0 << " ";
}else{
fout << dist[i] << " ";
}
}
return 0;
}