Pagini recente » Cod sursa (job #1023887) | Cod sursa (job #1738832) | Cod sursa (job #366644) | Cod sursa (job #1662300) | Cod sursa (job #2046718)
#include <bits/stdc++.h>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int N, M;
int D[50001];
vector<pair<int, int>> G[50001];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> PQ;
void Read() {
f >> N >> M;
for (int i = 1; i <= M; i++) {
int x, y, c;
f >> x >> y >> c;
G[x].push_back({c, y});
G[y].push_back({c, x});
}
}
void Dijkstra() {
D[1] = 0;
for (int i = 2; i <= N; i++) D[i] = -1;
for (auto it:G[1])
PQ.push(it),
D[it.second] = it.first;
while (!PQ.empty()) {
pair<int, int> current = PQ.top();
PQ.pop();
for (auto next:G[current.second])
if (D[next.second]==-1 && D[next.second] > D[current.second] + next.first)
D[next.second] = D[current.second] + next.first,
PQ.push(next); }
for (int i = 2; i <= N; i++) if (D[i] == INT_MAX) g << "0 ";
else g << D[i] << ' ';
}
int main() {
Read();
Dijkstra();
}