Pagini recente » Cod sursa (job #3253473) | Cod sursa (job #3182926) | Cod sursa (job #2750954) | Cod sursa (job #1171204) | Cod sursa (job #3147854)
// dijkstra.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
vector < pair < int, int> > g[50005];//lol this is lowkey a matrix
const int oo = (1 << 30);
int n, m;
int D[50005];
bool InCoada[50005];
void citeste()
{
int x, y, c;
fin >> n >> m;
for (int i = 0; i < m; i++)
{
fin >> x >> y >> c;
g[x].push_back({ y,c });
}
}
struct cmp
{
bool operator()(int x, int y)
{
return D[x] > D[y];
}
};
priority_queue < int, vector < int >, cmp> Coada;
void dijkstra(int nodStart)//seamana cu lee
{
for (int i = 1; i <= n; i++)
D[i] = oo;
D[nodStart] = 0;
InCoada[nodStart] = true;
Coada.push(nodStart);
while (!Coada.empty())
{
int nodCurent = Coada.top();
Coada.pop();
InCoada[nodCurent] = false;
for (auto& i : g[nodCurent])
{
int vecin = i.first;
int cost = i.second;
if (D[nodCurent] + cost < D[vecin])
{
D[vecin] = D[nodCurent] + cost;
if (InCoada[vecin] == false)
{
InCoada[vecin] = true;
Coada.push(vecin);
}
}
}
}
}
void afish()
{
for (int i = 2; i <= n; i++)
{
if (D[i] != oo)
fout << D[i] << " ";
else
fout << "0 ";
}
}
int main()
{
citeste();
dijkstra(1);
afish();
return 0;
}