Pagini recente » Cod sursa (job #389375) | Cod sursa (job #1178619) | Cod sursa (job #1823213) | Cod sursa (job #42151) | Cod sursa (job #3152779)
#include <bits/stdc++.h>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
const int Nmax=5e4+5;
const long long inf = 1e15;
vector< pair< long long,int>>G[Nmax];
long long dist[Nmax],v[Nmax];
int n,m;
int main()
{ cout<<INT_MAX;
f>>n>>m;
for(int i=1,x,y,c; i<=m; i++)
{
f>>x>>y>>c;
G[x].push_back({c,y});
}
for(int i=2; i<=n; i++)
dist[i]=inf;
priority_queue<pair<long long,int >> heap;
heap.push({0,1});
dist[1]=0;
while(!heap.empty())
{
int u=heap.top().second;
if(v[u]==0)
{
v[u]=1;
for(int i=0; i<G[u].size(); i++)
{
long long cost=G[u][i].first;
int nod=G[u][i].second;
if(dist[nod]>dist[u]+cost)
{
dist[nod]=dist[u]+cost;
heap.push({-dist[nod],nod});
}
}
}
heap.pop();
}
for(int i=2; i<=n; i++)
if(dist[i]==inf)
g<<0<<' ';
else
g<<dist[i]<<' ';
return 0;
}