Pagini recente » Cod sursa (job #2819318) | Cod sursa (job #1512272) | Cod sursa (job #1115015) | Cod sursa (job #1473187) | Cod sursa (job #2793741)
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
ifstream f ("dijkstra.in");
ofstream g ("dijkstra.out");
vector<pair<int,int>> la[50005];
unsigned dist[50005];
int n,m;
unsigned maxUnsigned = 0-1;
void dijkstra(int s)
{
dist[s] = 0;
queue<pair<int,int>> pq;
pq.push({0, s});
while (pq.size())
{
pair<int,int> top = pq.front();
pq.pop();
for (auto el: la[top.second])
{
if (dist[el.first] > dist[top.second] + el.second)
{
dist[el.first] = dist[top.second] + el.second;
pq.push({-el.second, el.first});
}
}
}
}
int main()
{
f >> n >> m;
for (int i = 0; i < m; i++)
{
int x,y,d;
f >> x >> y >> d;
la[x].push_back({y,d});
}
for (int i = 1; i <= n; i++)
dist[i] = 0 - 1;
dijkstra(1);
for (int i = 2; i <= n; i++)
{
if (maxUnsigned == dist[i])
dist[i] = 0;
g << dist[i] << ' ';
}
return 0;
}