Pagini recente » Cod sursa (job #2933198) | Cod sursa (job #1119228) | Cod sursa (job #807648) | Cod sursa (job #1018969) | Cod sursa (job #2977680)
#include <iostream>
#include <fstream>
#include <queue>
#define INF 20005
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
struct pereche {
int nod;
int d;
friend bool operator<(const pereche &a, const pereche &b) {
return a.d<b.d;
}
};
priority_queue<pereche> Q;
vector<pair<int, int>> G[50005];
int n, m, dmin[50005], viz[50005];
void citire() {
fin>>n>>m;
int x, y, c;
for (int i=0; i<m; i++) {
fin>>x>>y>>c;
G[x].emplace_back(y, c);
}
}
void add_pair(int nod, int d) {
pereche aux;
aux.nod=nod;
aux.d=d;
Q.push(aux);
}
void dijkstra(int start) {
for (int i=1; i<=n; i++) {
dmin[i]=INF;
}
dmin[start]=0;
viz[start]=1;
add_pair(start, 0);
while (!Q.empty()) {
int nod=Q.top().nod;
Q.pop();
for (auto i: G[nod]) {
int alt=dmin[nod]+i.second;
if (alt<dmin[i.first]) {
dmin[i.first]=alt;
if (!viz[i.first]) {
add_pair(i.first, alt);
viz[i.first]=1;
}
}
}
}
}
void afisare() {
for (int i=2; i<=n; i++)
if (dmin[i]==INF)
fout<<"0 ";
else
fout<<dmin[i]<<" ";
}
int main() {
citire();
dijkstra(1);
afisare();
return 0;
}