Pagini recente » Cod sursa (job #1229509) | Cod sursa (job #2863632) | Cod sursa (job #977683) | Cod sursa (job #824581) | Cod sursa (job #2637082)
#include <fstream>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m, dist[50005];
vector<pair<int, int> > g[50005];
void citire() {
fin >> n >> m;
while(m--) {
int a, b, c;
fin >> a >> b >> c;
g[a].push_back({b, c});
}
}
void solve() {
for(int i = 1; i <= n; i++) dist[i] = -1;
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;
q.push({0,1});
while(!q.empty()) {
int curr = q.top().second;
int d = q.top().first;
q.pop();
if(dist[curr] != -1) continue;
dist[curr] = d;
for(auto next: g[curr])
q.push({dist[next.first], next.first});
}
}
void afis() {
for(int i = 2; i <= n; i++)
fout << ((dist[i] == -1) ? 0 : dist[i]) << ' ';
}
int main() {
citire();
solve();
afis();
}