Pagini recente » Cod sursa (job #231626) | Cod sursa (job #1721131) | Cod sursa (job #1195770) | Cod sursa (job #107041) | Cod sursa (job #1252302)
#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 <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(1);
while (Q.size() > 0) {
nod = Q.front();
Q.pop();
costcrt = sol[nod];
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(nod2);
}
}
}
}
void scrie() {
for (int i = 2; i <= n; i++) {
if (sol[i] == INF) sol[i] = 0;
g << sol[i] << ' ';
}
}
int main() {
citeste();
rezolva();
scrie();
return 0;
}