Pagini recente » Cod sursa (job #72527) | Cod sursa (job #2161944) | Cod sursa (job #1697507) | Cod sursa (job #1310356) | Cod sursa (job #3305830)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int NMAX=5e4, INF=125e8+5;
vector<pair<int,int> > mat[NMAX+5];
vector<bool> vizitat(NMAX+5);
vector<int> dist(NMAX+5,INF);
void dijkstra(int nod){
priority_queue<pair<int,int> > q;
q.push({0, nod});
while(!q.empty()){
nod=q.top().second;
q.pop();
if(vizitat[nod]==0){
for(auto it:mat[nod]){
if(dist[nod]+it.second<dist[it.first]){
dist[it.first]=dist[nod]+it.second;
q.push({-dist[it.first], it.first});
}
}
vizitat[nod]=1;
}
}
}
int main(){
int n, m;
fin>>n>>m;
for(int i=1;i<=m;i++){
int x, y, val;
fin>>x>>y>>val;
mat[x].push_back({y, val});
}
dist[1]=0;
dijkstra(1);
for(int i=2;i<=n;i++){
if(dist[i]!=INF){
fout<<dist[i]<<' ';
}else{
fout<<0<<' ';
}
}
}