Pagini recente » Istoria paginii utilizator/stay_awake77 | Visuian Mihai | Profil robertpoe | Cod sursa (job #2163706) | Cod sursa (job #2072380)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define NM 50001
#define INF 2000000000
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
struct arc {
int y, c;
};
int d[NM], n, m, p;
bool inq[NM];
struct compar {
bool operator() (int x, int y) {
return d[x] > d[y];
}
};
vector <arc> G[NM];
priority_queue <int, vector <int>, compar> q;
void citire () {
arc u;
int x, y;
fin >> n >> m >> p;
for (int i = 1; i <= m; ++i) {
fin >> x >> y >> u.c;
u.y = y;
G[x].push_back(u);
u.y = x;
G[y].push_back(u);
}
}
void dijkstra () {
int i, im;
int y, c, lg;
for (i = 1; i <= n; ++i) d[i] = INF;
d[p] = 0;
q.push(p);
inq[p] = true;
while (!q.empty()) {
im = q.top();
q.pop();
inq[im] = false;
for (i = 0; i < G[im].size(); ++i) {
y = G[im][i].y;
c = G[im][i].c;
lg = d[im] + c;
if (lg < d[y]) {
d[y] = lg;
if (!inq[y]) {
q.push(y);
inq[y] = true;
}
}
}
}
}
void afisare () {
for (int i = 1; i <= n; ++i)
if (d[i] != INF) fout << d[i] << ' ';
else fout << -1 << ' ';
}
int main() {
citire();
dijkstra();
afisare();
return 0;
}