Pagini recente » Cod sursa (job #1173156) | Cod sursa (job #1326120) | Cod sursa (job #1769326) | Cod sursa (job #2178807) | Cod sursa (job #2420920)
#include <bits/stdc++.h>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
const int NMAX = 50005;
const int inf = 1e9;
struct muchie{
int node, cost;
};
priority_queue< pair<int,int> > q;
vector <pair <int,int > > v[NMAX];
int n,m,dist[NMAX];
bool viz[NMAX];
int main(){
int i,a,b,c;
muchie x;
f >> n >> m;
for(i = 1 ; i <= m ; i++){
f >> a >> b >> c;
v[a].push_back({b,c});
}
for(i = 2 ; i <= n ; i++)
dist[i] = inf;
q.push({0,1});
while(!q.empty()){
int nod = q.top().second;
q.pop();
if(viz[nod])
continue;
for(i = 0 ; i < v[nod].size() ; i++){
int value = dist[nod] + v[nod][i].second;
if(dist[v[nod][i].first] > value){
dist[v[nod][i].first] = value;
q.push({-dist[v[nod][i].first],v[nod][i].first});
}
}
}
for(i = 2 ; i <= n ; i++)
if(dist[i] == inf)
g << 0 << " ";
else
g << dist[i] << " ";
return 0;
}