Pagini recente » Cod sursa (job #3269970) | Cod sursa (job #840676) | Cod sursa (job #1653401) | Cod sursa (job #1153523) | Cod sursa (job #2539352)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
const int oo = (1 << 30);
const int NMAX = 50005;
vector < pair <int,int> > G[NMAX];
int N, M, D[NMAX];
bool InCoada[NMAX];
struct compara
{
bool operator()(int x, int y)
{
return D[x] > D[y];
}
};
priority_queue<int, vector<int>, compara> Coada;
void dijkstra(int nodStart) {
for(int i = 1; i <= N; ++i)
D[i] = oo;
D[nodStart] = 0;
Coada.push(nodStart);
InCoada[nodStart] = 1;
while(! Coada.empty()) {
int nodCurent = Coada.top();
Coada.pop();
InCoada[nodCurent] = 0;
for(size_t i = 0; i < G[nodCurent].size(); ++i) {
int Vecin = G[nodCurent][i].first;
int Cost = G[nodCurent][i].second;
if(D[nodCurent] + Cost < D[Vecin]) {
D[Vecin] = D[nodCurent] + Cost;
if(!InCoada[Vecin]){
Coada.push(Vecin);
InCoada[Vecin] = 1;
}
}
}
}
for(int i = 2; i <= N; ++i)
if(oo != D[i])
out << D[i] << ' ';
else
out << 0 << ' ';
}
int main() {
in >> N >> M;
for(int i = 1; i <= M; ++i){
int x, y, c;
in >> x >> y >> c;
G[x].push_back(make_pair(y, c));
}
dijkstra(1);
return 0;
}