Pagini recente » Cod sursa (job #1973837) | Cod sursa (job #1268952) | Cod sursa (job #42721) | Cod sursa (job #888740) | Cod sursa (job #2549636)
#include <bits/stdc++.h>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
const int N = 50010;
const int oo = 1000000000;
vector <pair<int, int>> v[N];
priority_queue <pair<int, int>> pq;
int d[N], n, m;
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}); /// cost , nod
while(!pq.empty())
{
int nod, cost;
tie(cost, nod) = pq.top();
cost = -cost;
pq.pop();
if(d[nod] == cost)
{
for(auto it: v[nod])
{
if(d[it.first] > cost + it.second)
{
d[it.first] = cost + it.second;
pq.push({-d[it.first], it.first});
}
}
}
}
for(int i = 2; i <= n; i++)
{
if(d[i] == oo)
g << 0 << ' ';
else
g << d[i] << ' ';
}
return 0;
}