Pagini recente » Cod sursa (job #2701234) | Cod sursa (job #2203226) | Cod sursa (job #2507855) | Cod sursa (job #614654) | Cod sursa (job #2755574)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int n,m,vf_curent = 1;
bool viz[50001];
int tati[50001],distante[50001];
vector<pair<int,int>>adiacenta[50001];
int gasire_vf_cu_pasi_minimi()
{
int rasp = -1,pasi_minimi = 1000000;
for(int i = 1; i<=n; i++)
{
if(!viz[i] && distante[i]!= -1 && distante[i]<pasi_minimi)
{
rasp = i;
pasi_minimi = distante[i];
}
}
return rasp;
}
int main()
{
f >> n >> m;
for(int i = 1; i<=m; i++)
{
int A,B,C;
f >> A >> B >> C;
adiacenta[A].push_back({B,C});
}
for(int i = 1; i<=50000; i++)
distante[i] = -1;
distante[1] = 0;
vf_curent = gasire_vf_cu_pasi_minimi();
while(vf_curent != -1)
{
viz[vf_curent] = 1;
for(int i = 0; i<adiacenta[vf_curent].size(); i++)
{
int nod = adiacenta[vf_curent][i].first;
int cost = adiacenta[vf_curent][i].second;
if(!viz[nod])
{
if(distante[nod] > distante[vf_curent] + cost || distante[nod] == -1)
{
distante[nod] = distante[vf_curent] + cost;
tati[nod] = vf_curent;
}
}
}
vf_curent = gasire_vf_cu_pasi_minimi();
}
for(int i = 2; i<=n; i++)
{
if(distante[i] == -1)
g << "0 ";
else
g << distante[i]<< " ";
}
}