Pagini recente » Cod sursa (job #1758408) | Cod sursa (job #1311931) | Cod sursa (job #1493997) | Cod sursa (job #2266019) | Cod sursa (job #2637078)
#include <fstream>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int inf = 1000000000;
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 = 2; i <= n; i++) dist[i] = inf;
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();
for(auto next: g[curr])
if(dist[next.first] > dist[curr]+next.second) {
dist[next.first] = dist[curr]+next.second;
q.push({dist[next.first], next.first});
}
}
}
void afis() {
for(int i = 2; i <= n; i++)
fout << dist[i] == inf ? 0 : dist[i] << ' ';
}
int main() {
citire();
solve();
afis();
}