Pagini recente » Cod sursa (job #1376443) | Cod sursa (job #2065746) | Cod sursa (job #2358844) | Cod sursa (job #2029039) | Cod sursa (job #1906871)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
typedef pair<int, int> edge;
int n, m, d[50001];
bool viz[50001];
vector<edge> g[50001];
priority_queue<edge, vector<edge>, greater<edge> > q;
const int INF = 2e9 + 1;
void citire();
int main()
{
citire();
q.push({0, 1});
while (!q.empty()) {
int x = q.top().second;
int cost = q.top().first;
q.pop();
if (viz[x]) continue;
viz[x] = true;
for (int i = 0; i < g[x].size(); ++i) {
int y = g[x][i].second;
if (!viz[y]) {
if (cost + g[x][i].first < d[y]) {
d[y] = cost + g[x][i].first;
q.push({d[y], y});
}
}
}
}
for (int i = 2; i <= n; ++i) {
if (d[i] == INF) {
out << 0 << " ";
} else {
out << d[i] << " ";
}
}
return 0;
}
void citire() {
in >> n >> m;
for (int i = 1, x, y, cost; i <= m; ++i) {
in >> x >> y >> cost;
g[x].push_back({cost, y});
}
for (int i = 1; i <= n; ++i)
d[i] = INF;
in.close();
}