Pagini recente » Cod sursa (job #376855) | Cod sursa (job #1162008) | Cod sursa (job #2767095) | Cod sursa (job #2459385) | Cod sursa (job #2280194)
#include <bits/stdc++.h>
using namespace std;
ifstream f("djkistra.in");
ofstream g("djkistra.out");
const int INF = 1e9;
const int NMax = 5e4 + 5;
int n, m;
vector < pair < int, int > > G[NMax];
int D[Nmax];
void djkistra(){
priority_queue < pair < int, int >, vector < pair < int, int > >, greater < pair < int, int > > > pq;
pq.push({0, 1});
while (!pq.empty()){
int node = pq.top().second;
int value = pq.top().first;
pq.pop();
if(D[node] != value) continue;
for(auto const &it: G[node]){
if (value + it.second < D[it.first]){
D[it.first] = value + it.second;
pq.push({dist[it.first], it.first});
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
f >> n >> m;
for(int i = 1; i <= m; i++) {
int a, b, c;
f >> a >> b >> c;
G[a].push_back({b, c});
}
for(int i = 2; i <= n; i++) D[i] = INF;
Dijkstra();
for(int i = 2; i <= n; i++) {
if(D[i] == INF) {
g << "0 ";
} else {
g << D[i] << " ";
}
}
return 0;
}