Pagini recente » Cod sursa (job #2059780) | Cod sursa (job #3255120) | Cod sursa (job #2106200) | Cod sursa (job #2317670) | Cod sursa (job #2365526)
#include <bits/stdc++.h>
#define nmax 50007
#define oo 1e9
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
int n, m;
struct Db
{
int nod, cost;
bool operator < (const Db &A) const
{
return cost > A.cost;
}
};
priority_queue <Db> q;
vector <Db> L[nmax];
int viz[nmax], drum[nmax];
void Citire()
{
int i, x, y, c;
fin >> n >> m;
for (i = 1; i <= m; i++)
{
fin >> x >> y >> c;
L[x].push_back({y, c});
}
}
void Dijkstra()
{
int i, el, nod, cost;
for (i = 1; i <= n; i++)
drum[i] = oo;
drum[1] = 0;
q.push({1, 0});
while (!q.empty())
{
el = q.top().nod;
q.pop();
if (viz[el] == 0)
{
viz[el] = 1;
for (auto w : L[el])
{
nod = w.nod;
cost = w.cost;
if (drum[nod] > drum[el] + cost)
{
drum[nod] = drum[el] + cost;
q.push({nod, drum[nod]});
}
}
}
}
}
int main()
{
Citire();
Dijkstra();
for (int i = 2; i <= n; i++)
{
if (drum[i] == oo)
drum[i] = 0;
fout << drum[i] << " ";
}
fout << "\n";
fin.close();
fout.close();
return 0;
}