Pagini recente » Cod sursa (job #173036) | Cod sursa (job #1878550) | Cod sursa (job #1615178) | Cod sursa (job #2559953) | Cod sursa (job #3261343)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int NMAX=50005;
const int INF=2000000000;
int n, m, x, y, c;
vector <pair <int, int>> a[NMAX];
priority_queue <pair <int,int>> heap;
vector <int> dist(NMAX, INF);
bitset <NMAX> viz;
int main(){
fin>>n>>m;
for(int i=1;i<=m;i++){
fin>>x>>y>>c;
a[x].push_back({y, c});
}
dist[1]=0;
heap.push({0,1});
while(!heap.empty()){
int inter=heap.top().second;
int d=-heap.top().first;
heap.pop();
if(!viz[inter]){
for(pair<int,int> urm : a[inter]){
if(dist[urm.first]>dist[inter]+urm.second){
dist[urm.first]=dist[inter]+urm.second;
heap.push({-dist[urm.first],urm.first});
}
}
viz[inter]=1;
}
}
for(int i=2;i<=n;i++){
if(dist[i]!=INF)
fout<<dist[i]<<' ';
else
fout<<0<<' ';
}
return 0;
}