Pagini recente » Cod sursa (job #317483) | Cod sursa (job #1605737) | Cod sursa (job #140699) | Cod sursa (job #1309018) | Cod sursa (job #1252296)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f ("dijkstra.in");
ofstream g ("dijkstra.out");
const int NMAX = 50000 + 1;
const int INF = 0x7fffffff;
int n, m;
int sol[NMAX];
vector <int> graf[NMAX], cost[NMAX];
queue < pair <int, int> > Q;
void citeste() {
int a, b, c;
f >> n >> m;
for (int i = 1; i <= m; i++) {
f >> a >> b >> c;
graf[a].push_back(b);
cost[a].push_back(c);
}
}
void rezolva() {
int costcrt, nod, nod2, l;
for (int i = 2; i <= n; i++) sol[i] = INF;
Q.push(make_pair(0, 1));
while (Q.size() > 0) {
costcrt = (Q.front()).first;
nod = (Q.front()).second;
Q.pop();
l = graf[nod].size();
for (int i = 0; i < l; i++) {
nod2 = graf[nod][i];
if (sol[nod2] > costcrt + cost[nod][i]) {
sol[nod2] = costcrt + cost[nod][i];
Q.push(make_pair(sol[nod2], nod2));
}
}
}
for (int i = 2; i <= n; i++) {
if (sol[i] == INF) sol[i] = 0;
g << sol[i] << ' ';
}
}
int main() {
citeste();
rezolva();
return 0;
}