Pagini recente » Cod sursa (job #2745907) | Cod sursa (job #280046) | Cod sursa (job #1745441) | Cod sursa (job #1764482) | Cod sursa (job #3335853)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int nmax = 5e4;
int n, m;
vector< pair<int, int> > adj[nmax + 1];
void citire() {
fin >> n >> m;
for(int i = 1; i <= m; i ++) {
int x, y, c;
fin >> x >> y >> c;
adj[x].push_back({y, c});
}
}
priority_queue < pair<int, int> > pq;
int d[nmax + 1];
const int inf = 999999999;
void dijkstra(int start) {
for(int i = 1; i <= n; i ++) {
d[i] = inf;
}
d[start] = 0;
pq.push({0, start});
while (!pq.empty()) {
int nod = pq.top().second;
int cost_curent = (-1) * pq.top().first;
pq.pop();
for (auto &v : adj[nod]) {
int vecin = v.first;
int cost = v.second;
if (d[vecin] > d[nod] + cost) {
d[vecin] = d[nod] + cost;
pq.push({(-1) * d[vecin], vecin});
}
}
}
}
int main() {
citire();
int start = 1;
dijkstra(start);
for(int i = 1; i <= n; i ++) {
if (i == start) {
continue;
}
if (d[i] < inf)
fout << d[i] << " ";
else {
fout << "0 ";
}
}
return 0;
}