Pagini recente » Cod sursa (job #629435) | Cod sursa (job #2293204) | Cod sursa (job #696770) | Cod sursa (job #2328585) | Cod sursa (job #2866875)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int NMax = 50005, inf = (1 << 30);
int D[NMax];
bool InCoada[NMax];
int N, M;
vector<pair<int, int>> G[NMax];
struct compara
{
bool operator()(int x, int y)
{
return D[x] > D[y];
}
};
priority_queue<int, vector<int>, compara> 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({y, c});
}
}
void dijkstra(int nodStart)
{
for(int i = 1; i <= N; i++)
D[i] = inf;
D[nodStart] = 0;
Coada.push(nodStart);
InCoada[nodStart] = true;
while(!Coada.empty())
{
int nodCurent = Coada.top();
Coada.pop();
InCoada[nodCurent] = false;
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] == false)
{
InCoada[Vecin] = true;
Coada.push(Vecin);
}
}
}
}
void afiseaza()
{
for(int i = 2; i <= N; i++)
{
if(D[i] != inf)
fout << D[i] << ' ';
else
fout << 0 << ' ';
}
}
int main()
{
citeste();
dijkstra(1);
afiseaza();
return 0;
}