Pagini recente » Cod sursa (job #398450) | Profil rares_ciociea | Cod sursa (job #338064) | Istoria paginii runda/simulare_shumen_1/clasament | Cod sursa (job #2576031)
#include <bits/stdc++.h>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
const int N = 50010;
const int oo = 1000000010;
vector <pair<int, int>> v[N];
priority_queue <pair<int, int>> pq;
int n, m, d[N];
int main()
{
f >> n >> m;
for(; m; m--)
{
int x, y, c;
f >> x >> y >> c;
v[x].push_back({y, c});
}
for(int i = 2; i <= n; i++)
d[i] = oo;
pq.push({0, 1});
while(!pq.empty())
{
int nod, cost;
tie(cost, nod) = pq.top();
pq.pop();
cost = -cost;
if(d[nod] == cost)
{
for(auto it:v[nod])
{
if(d[it.first] > it.second + cost)
{
d[it.first] = it.second + cost;
pq.push({-d[it.first], it.first});
}
}
}
}
for(int i = 2; i <= n; i++)
if(d[i] != oo)
g << d[i] << ' ';
else
g << 0 << ' ';
return 0;
}