Pagini recente » Cod sursa (job #1178485) | Cod sursa (job #1444163) | Cod sursa (job #11972) | Cod sursa (job #1283431) | Cod sursa (job #2362853)
#include <fstream>
#include <queue>
#include <vector>
#include <functional>
#define deb 0
#define deb2 1
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int oo = 2000000000;
const int nMax = 50000;
vector<pair<int, int>> g[nMax + 5];
int n, m;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> heap;
int dp[nMax + 5], use[nMax + 5];
void Read() {
fin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y, c;
fin >> x >> y >> c;
g[x].push_back({y, c});
}
}
void Dijkstra() {
for (int i = 2; i <= n; i++)
dp[i] = oo;
heap.push({0, 1});
use[1] = 1;
while (!heap.empty()) {
int nod = heap.top().second;
int cost = heap.top().first;
heap.pop();
if (dp[nod] != cost)
continue;
for (auto i : g[nod]) {
if (dp[i.first] > cost + i.second) {
dp[i.first] = cost + i.second;
heap.push({dp[i.first], i.first});
}
}
}
}
int main() {
Read();
Dijkstra();
for (int i = 2; i <= n; i++)
if (dp[i] == oo)
fout << 0 << " ";
else
fout << dp[i] << " ";
fout << '\n';
return 0;
}