Pagini recente » Cod sursa (job #2456029) | Cod sursa (job #528418) | Cod sursa (job #2604857) | Cod sursa (job #2351270) | Cod sursa (job #2537972)
#include <bits/stdc++.h>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
const int dim = 50001;
const int oo = 200000001;
int n, m;
vector < pair <int, int> > v[dim];
int dist[dim];
void dijkstra(int nods){
priority_queue < pair <int, int> > pq;
pq.push({0, nods});
while(!pq.empty()){
auto frnt = pq.top(); pq.pop();
for(auto it: v[frnt.second]){
if(dist[frnt.second] + it.second < dist[it.first]){
dist[it.first] = dist[frnt.second] + it.second;
pq.push({-dist[it.first], it.first});
}
}
}
}
int main()
{
int i, j, a, b, c;
f >> n >> m;
for(i = 1; i <= m; ++i){
f >> a >> b >> c;
v[a].push_back({b, c});
}
fill_n(dist + 2, n + 1, oo);
dijkstra(1);
for(i = 2; i <= n; ++i)
g << (dist[i] < oo ? dist[i] : 0) << " ";
return 0;
}