Pagini recente » Cod sursa (job #615217) | Cod sursa (job #2515533) | Cod sursa (job #1885357) | Cod sursa (job #2987235) | Cod sursa (job #2976711)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
string np = "dijkstra";
ifstream f(np + ".in");
ofstream g(np + ".out");
// #define f cin
// #define g cout
int n, m;
const ll INF = 1e16L;
vector<ll> d;
vector<pair<int, int>> adj[50003];
void djikstra(int nod)
{
priority_queue<pair<ll, int>> q;
d.assign(n + 1, INF);
q.push({0LL, nod});
d[nod] = 0;
while (!q.empty())
{
int curr = q.top().second;
if (-q.top().first > d[curr])
{
q.pop();
continue;
}
q.pop();
for (auto next : adj[curr])
if (d[next.first] > d[curr] + next.second)
d[next.first] = d[curr] + next.second,
q.push({-d[next.first], next.first});
}
}
int main()
{
ios_base::sync_with_stdio(false);
f.tie(nullptr);
f >> n >> m;
for (int a, b, val; f >> a >> b >> val;)
adj[a].push_back({b, val});
djikstra(1);
for (int i = 2; i <= n; i++)
if (d[i] == INF)
g << "0 ";
else
g << d[i] << " ";
return 0;
}