Pagini recente » Cod sursa (job #2625104) | Cod sursa (job #2447816) | Cod sursa (job #66171) | Cod sursa (job #171588) | Cod sursa (job #1252294)
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
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];
set < pair <int, int> > t;
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;
t.insert(make_pair(0, 1));
while (t.size() > 0) {
costcrt = (*t.begin()).first;
nod = (*t.begin()).second;
t.erase(*t.begin());
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];
t.insert(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;
}