Pagini recente » Cod sursa (job #898145) | Cod sursa (job #921177) | Cod sursa (job #1483604) | Cod sursa (job #3323735) | Cod sursa (job #3323733)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int inf = 1<<31-1;
ifstream fin("");
ofstream fout("");
vector<vector<pair<int, int>>> adj_list;
vector<int> viz;
vector<int> dist;
priority_queue<pair<int,int>, vector<pair<int,int>, greater<pair<int,int>>>> pq;
int n, m;
void dijkstra(int nod_start, int n)
{
viz.assign(n+1, 0);
dist.assign(n+1, inf);
dist[nod_start] = 0;
pq.push({dist[nod_start], nod_start});
while ( !pq.empty() )
{
auto nod_curent = pq.top();
pq.pop();
viz[nod_curent.second] = 1;
for ( auto &v : adj_list[nod_curent.second] )
{
if ( !viz[v.first] )
{
if ( dist[v.first] > dist[nod_curent.second] + v.second )
{
dist[v.first] = dist[nod_curent.second] + v.second;
pq.push({dist[v.first], v.first});
}
}
}
}
for ( int i = 2; i <= n; i++ )
{
if ( dist[i] == inf)
{
fout << 0;
}
fout<<dist[i]<<" ";
}
}
int main()
{
fin>>n>>m;
adj_list.assign(n+1, {});
for ( int i = 0; i < m; i++ ) {
int x,y,z;
fin >> x >> y >> z;
adj_list[x].push_back({y,x});
}
dijkstra(1,n);
return 0;
}