Pagini recente » Cod sursa (job #2663648) | Cod sursa (job #2934942) | Cod sursa (job #3033303) | Cod sursa (job #995803) | Cod sursa (job #2955505)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<>>q;
vector<vector<pair<int,int>>>gr;
vector<int>distMin;
const int inf=1e9;
int main()
{ int n,m;
cin>>n>>m;
distMin.resize(n+1);
gr.resize(n+1);
for(int i=2;i<=n;i++){
distMin[i]=inf;
}
int a,b,c;
while(m--){
cin>>a>>b>>c;
gr[a].push_back({b,c});
}
q.push({0,1});
while(!q.empty()){
pair<int,int> nodCurent=q.top();
q.pop();
int distCurenta=nodCurent.first;
int nod=nodCurent.second;
if(distCurenta!=distMin[nod]){
continue;
}
for(auto&x:gr[nod]){
if(distMin[x.first]>distCurenta+x.second){
distMin[x.first]=distCurenta+x.second;
q.push({distMin[x.first],x.first});
}
}
}
for(int i=2;i<=n;i++){
if(distMin[i]==inf){
cout<<0<<" ";
}else{
cout<<distMin[i]<<" ";
}
}
return 0;
}