Pagini recente » Cod sursa (job #276619) | Cod sursa (job #571044) | Cod sursa (job #1967538) | Cod sursa (job #3219288) | Cod sursa (job #2174745)
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
const int NMax = 50005;
const int inf = 0x3f3f3f3f;
vector < pair < int, int > > G[NMax];
priority_queue < pair < int, int > > Q;
int N, M;
int dist[NMax];
void Dijk()
{
Q.push({0,1});
memset(dist,inf,sizeof(dist));
dist[1] = 0;
while(!Q.empty())
{
int cost = -Q.top().first;
int nodc = Q.top().second;
Q.pop();
if(cost > dist[nodc])
continue;
for(auto it: G[nodc])
{
if(dist[it.first] > cost + it.second)
{
dist[it.first] = cost + it.second;
Q.push({-dist[it.first],it.first});
}
}
}
for(int i=2; i<=N; ++i)
{
if(dist[i]!=inf)
cout << dist[i] << " ";
else
cout << "0 ";
}
}
int main()
{
freopen("dijkstra.in", "r", stdin);
freopen("dijkstra.out", "w", stdout);
scanf("%d%d",&N,&M);
for(int i=1; i<=M; ++i)
{
int x,y,c;
scanf("%d%d%d", &x,&y,&c);
G[x].push_back({y,c});
}
Dijk();
return 0;
}