Pagini recente » Cod sursa (job #2811206) | Cod sursa (job #3124256) | Cod sursa (job #2256993) | Cod sursa (job #3182595) | Cod sursa (job #2448392)
#include<bits/stdc++.h>
#include <fstream>
using namespace std;
#define maxn 50005
#define INF 1 << 30
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int N, M;
std::vector<pair<int, int>> v[maxn];
int cost[maxn];
int verif[maxn];
struct comp{
bool operator()(int x,int y){
return cost[x]>cost[y];
}
};
priority_queue <int, vector<int>, comp> q;
void dij(int nod){
for(int i = 1; i <= N; i++){
cost[i] = INF;
}
cost[nod] = 0;
verif[nod] = 1;
q.push(nod);
while(!q.empty()){
int a = q.top();
verif[a]=0;
q.pop();
for(int i = 0; i < v[a].size(); i++){
int b = v[a][i].first;
int c = v[a][i].second;
if(cost[b]>cost[a]+c){
cost[b]=cost[a]+c;
if(verif[b]==0){
verif[b]=1;
q.push(b);
}
}
}
}
}
int main(){
fin >> N >> M;
for(int i = 1; i <= M; i++){
int x, y, val;
fin >> x >> y >> val;
v[x].push_back(make_pair(y, val));
}
dij(1);
for(int i = 2; i <= N; i++){
if(cost[i]==INF){
fout <<"0 ";
}else{
fout << cost[i]<<" ";
}
}
return 0;
}