Pagini recente » Cod sursa (job #2451248) | Cod sursa (job #520580) | Cod sursa (job #242453) | Cod sursa (job #2015991) | Cod sursa (job #3206883)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
#define MAXN 50010
#define INF 1000000000
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
vector<pair<int, int>> G[MAXN];
priority_queue<pair<int, int>> PQ;
int n, m, x, y, c;
int costuri[MAXN];
void dijkstra(int nod) {
for (int i = 1; i <= n; ++i) {
costuri[i] = INF;
}
costuri[nod] = 0;
PQ.push({0, nod});
while (PQ.size()) {
auto aux = PQ.top();
int nodCurrent = aux.second;
PQ.pop();
for (auto it:G[nodCurrent]) {
if (costuri[nodCurrent] + it.second < costuri[it.first]) {
costuri[it.first] = costuri[nodCurrent] + it.second;
PQ.push({-(costuri[nodCurrent] + it.second), it.first});
}
}
}
}
int main()
{
f >> n >> m;
while (m--) {
f >> x >> y >> c;
G[x].push_back({y, c});
G[y].push_back({x, c});
}
dijkstra(1);
for (int i = 2; i <= n; ++i) {
g << costuri[i] << ' ';
}
return 0;
}