Pagini recente » Cod sursa (job #2754582) | Cod sursa (job #604624) | Cod sursa (job #2918482) | Cod sursa (job #271326) | Cod sursa (job #2231753)
#include<bits/stdc++.h>
using namespace std;
// Part of Cosmos by OpenGenus Foundation
const int N = 50001;
vector<pair<int,int> > graph[N];
set<pair<int,int> > pq;
int dist[N],vis[N];
int main()
{
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
int n,m; cin>>n>>m;
while(m--)
{
int u,v,w; cin>>u>>v>>w;
graph[u].push_back({v,w});
}
for(int i = 0;i<=n;i++) dist[i] = 1E9, vis[i] = 0;
dist[1] = 0;
pq.insert({0,1});
while(!pq.empty())
{
pair<int,int> p = *pq.begin();
pq.erase(pq.begin());
int u = p.second;
if(vis[u]) continue;
vis[u] = 1;
for(auto k : graph[u])
{
if(dist[k.first]>(dist[u]+k.second))
{
dist[k.first] = dist[u] + k.second;
pq.insert({dist[k.first],k.first});
}
}
}
for(int i = 2;i<=n;i++) cout<<dist[i]<<" ";
return 0;
}