Pagini recente » Cod sursa (job #1501320) | Cod sursa (job #1837202) | Cod sursa (job #1259166) | Cod sursa (job #2198892) | Cod sursa (job #2830853)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
// cost nod
priority_queue< pair<int, int> > q;
int n, m;
int d[50003], viz[50003];
vector< pair<int, int> > h[50003];
const int oo = 1e9;
void Dijkstra(int sursa)
{
int i, k, cost, nod;
for (i = 1; i <= n; i++)
d[i] = oo;
d[sursa] = 0;
q.push( {0, sursa} );
while (!q.empty())
{
k = q.top().second;
q.pop();
if (viz[k] == 0)
{
viz[k] = 1;
for (auto e : h[k])
{
cost = e.first;
nod = e.second;
if (d[nod] > d[k] + cost)
{
d[nod] = d[k] + cost;
q.push( {-d[nod], nod} );
}
}
}
}
}
void Afis()
{
for (int i = 2; i <= n; i++)
if (d[i] == oo) fout << "0 ";
else fout << d[i] << " ";
}
void Citire()
{
int x, y, c;
fin >> n >> m;
for (int i = 1; i <= m; i++)
{
fin >> x >> y >> c;
h[x].push_back( {c, y} );
}
Dijkstra(1);
Afis();
}
int main()
{
Citire();
return 0;
}