Pagini recente » Cod sursa (job #922679) | Cod sursa (job #2303529) | Cod sursa (job #2240834) | Cod sursa (job #2720087) | Cod sursa (job #2275530)
#include <bits/stdc++.h>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int N, M, nod, D[50003], x, y, c;
struct nod_t2 {
int cost, y;
bool operator<(const nod_t2 &other) const {
return cost > other.cost;
}
};
vector<nod_t2> G[50003], sol;
priority_queue<nod_t2> H;
bool sel[50003];
void dijkstra(int nod = 1) {
sol.clear();
while(!H.empty()) H.pop();
for(int i = 1; i <= N; i++)
sel[i] = false, D[i] = INT_MAX;
sel[nod] = true;
for(auto i : G[nod]) {
H.push({i.cost, i.y});
}
for(int i = 1; i < N; i++) {
while(!H.empty() && sel[H.top().y])
H.pop();
nod = H.top().y;
sel[nod] = true;
D[nod] = H.top().cost;
for(auto j : G[nod])
if(!sel[j.y])
H.push({j.cost + D[nod], j.y});
}
}
int main()
{
f >> N >> M;
for(int i = 1; i <= M; i++) {
f >> x >> y >> c;
G[x].push_back({c, y});
}
dijkstra();
for(int i = 2; i <= N; i++)
g << D[i] << " ";
g << "\n";
return 0;
}