Pagini recente » Cod sursa (job #2420855) | Borderou de evaluare (job #1580020) | Borderou de evaluare (job #2521451) | Cod sursa (job #92161) | Cod sursa (job #3333699)
#include <bits/stdc++.h>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
const int Nx=5e4+5;
const long long inf=1e15;
vector <pair<long long ,int>> G[Nx];
long long dist[Nx],vi[Nx];
int n,m,s;
int main()
{ s=1;
f>>n>>m;
for(int i=0,u,v,c; i<m; i++)
{
f>>u>>v>>c;
G[u].push_back({c,v});
}
priority_queue<pair<long long,int>>heap;
for(int i=1; i<=n; i++)
dist[i]=inf;
heap.push({0,s});
dist[s]=0;
while(!heap.empty())
{
int u=heap.top().second;
if(vi[u]==0)
{
vi[u]=1;
for(int i=0; i<G[u].size(); i++)
{
long long cost=G[u][i].first;
int v=G[u][i].second;
if (dist[v]>dist[u]+cost)
{
dist[v]=dist[u]+cost;
heap.push({-dist[v],v});
}
}
}
heap.pop();
}
for(int i=2; i<=n; i++)
if(dist[i]>=inf)
g<<"0"<<' ';
else g<<dist[i]<<' ';
return 0;
}