Pagini recente » Cod sursa (job #1880069) | Cod sursa (job #2790328) | Cod sursa (job #1381926) | Cod sursa (job #1533969) | Cod sursa (job #2110849)
#include <bits/stdc++.h>
#define in "dijkstra.in"
#define out "dijkstra.out"
#define N 50003
#define oo 20003
using namespace std;
ifstream fin(in);
ofstream fout(out);
int n,m;
vector <pair <int,int> > L[N];
bitset <N> viz;
int d[N];
void Read()
{
int i,x,y,cost;
fin >> n >> m;
for (i = 1; i <= m; i++)
{
fin >> x >> y >> cost;
L[x].push_back({y,cost});
}
}
void Dijkstra()
{
int i,pas,minim,k,cost;
for (i = 1; i <= n; i++)
d[i] = oo;
d[1] = 0;
for (pas = 1; pas < n; pas++)
{
minim = oo; /// distanta minima
k = 0; /// nodul de distanta minima
/// caut minimul in d
for (i = 1; i <= n; i++)
if (d[i] < minim && viz[i] == 0)
{
minim = d[i];
k = i;
}
/// vad daca totusi se poate ajunge
if (minim == oo) return;
viz[k] = 1;
for (auto w : L[k])
{
i = w.first;
cost = w.second;
if (d[i] > d[k] + cost) d[i] = d[k] + cost;
/// am actualizat distanta minima la ceilalti vecini
}
}
}
void Afis()
{
int i;
for (i = 2; i <= n; i++)
if (d[i] == oo) fout << "0 ";
else fout << d[i] << " ";
fout << "\n";
}
int main()
{
Read();
Dijkstra();
Afis();
fin.close();
fout.close();
return 0;
}