Pagini recente » Cod sursa (job #510925) | Cod sursa (job #2255324) | Cod sursa (job #264400) | Cod sursa (job #3252584) | Cod sursa (job #2448357)
#include <iostream>
#include<bits/stdc++.h>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
#define maxn 50005
#define INF 0x3f3f3f3f
int N, M;
int x, y, val;
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];
}
};
void dij(int nod){
cost[nod] = 0;
verif[nod] = 1;
priority_queue <int, vector<int>, comp> q;
q.push(nod);
while(!q.empty()){
int a = q.top();
verif[a]=1;
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]){
verif[b]=1;
q.push(b);
}
}
}
}
int main(){
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
fin >> N >> M;
for(int i = 1; i <= M; i++){
fin >> x >> y >> val;
v[x].push_back(make_pair(y, val));
}
memset(cost, INF, maxn);
dij(1);
for(int i = 2; i <= N; i++){
if(cost[i]==INF){
fout <<"0 ";
}else{
fout << cost[i]<<" ";
}
}
return 0;
}