Pagini recente » Cod sursa (job #1834630) | Cod sursa (job #976734) | Cod sursa (job #2616130) | Cod sursa (job #1625342) | Cod sursa (job #2602053)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
const int NMax = 50005;
const int oo = 0x3F3F3F3F;
int n, m;
int D[NMax];
bool InCoada[NMax];
vector < pair < int, int > > G[NMax];
struct comparaDistante
{
bool operator()(int x, int y)
{
return D[x] > D[y];
}
};
priority_queue < int > Coada;
void citeste()
{
fin >> n >> m;
for(int i = 1; i <= m; i++)
{
int x, y, c;
fin >> x >> y >> c;
G[x].push_back(make_pair(y, c));
}
}
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(unsigned int i = 0; i < G[nodCurent].size(); i++)
{
int Vecin = G[nodCurent][i].first;
int Cost = G[nodCurent][i].second;
if(D[Vecin] > D[nodCurent] + Cost)
{
D[Vecin] = D[nodCurent] + Cost;
if(InCoada[Vecin] == 0)
{
Coada.push(Vecin);
InCoada[Vecin] = 1;
}
}
}
}
}
void afisare()
{
for(int i = 2; i <= n; i++)
{
if(D[i] != oo)
fout << D[i] << ' ';
else
fout << 0 << ' ';
}
}
int main()
{
citeste();
Dijkstra(1);
afisare();
}