Pagini recente » Cod sursa (job #2412828) | Cod sursa (job #2467337) | Cod sursa (job #673024) | Cod sursa (job #2772832) | Cod sursa (job #2665334)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
const int NMax = 50005;
const int oo = (1 << 30);
int N, M;
int D[NMax];
bool InCoada[NMax];
int p;
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()
{
int x, y, c;
cin >> N >> p;
while(cin >> 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] = true;
while(!Coada.empty())
{
int nodCurent = Coada.top();
Coada.pop();
InCoada[nodCurent] = false;
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] == false)
Coada.push(Vecin),
InCoada[Vecin] = true;
}
}
}
}
void Afiseaza()
{
for(int i = 1; i <= N; i++)
(D[i] != oo) ?
cout << D[i] << " " : cout << "-1 ";
}
int main()
{
Citeste();
Dijkstra(p);
Afiseaza();
}