Pagini recente » Cod sursa (job #676617) | Cod sursa (job #1504655) | Cod sursa (job #2559114) | Cod sursa (job #2705206) | Cod sursa (job #2560233)
#include <bits/stdc++.h>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
const int NMax = 50005;
const int oo = (1 << 30);
int N, p, M;
int D[NMax];
bool InCoada[NMax];
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()
{
f >> N >> M;
int x,y,c;
for(int i=1; i<=M; i++)
{
f>>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 = 2; i <= N; i++)
{
if(D[i] != oo)
g << D[i] << " ";
else
g <<-1;
}
}
int main()
{
Citeste();
Dijkstra(1);
Afiseaza();
}