Pagini recente » Cod sursa (job #1039411) | Cod sursa (job #811329) | Cod sursa (job #1958480) | Cod sursa (job #2666602) | Cod sursa (job #2375595)
#include <bits/stdc++.h>
using namespace std;
ifstream f ("dijkstra.in");
ofstream g ("dijkstra.out");
#define oo 1000000000
int n,m,x,y,c,cost[50005];
vector<pair < int, int > > v[50005];
priority_queue<pair < int, int> >q; ///COST si NOD
void Dijkstra(int x)
{
q.push(make_pair(0,x));
while(!q.empty()){
int cost0=-q.top().first;
int k=q.top().second;
q.pop();
if(cost[k]!=cost0)continue;
for(auto x:v[k])
if(cost[x.first] > cost[k]+x.second){
cost[x.first]=cost[k]+x.second;
q.push(make_pair(-cost[x.first],x.first));
}
}
}
int main()
{
f>>n>>m;
for(int i=1;i<=m;++i){
f>>x>>y>>c;
v[x].push_back(make_pair(y,c));
}
for(int i=2;i<=n;++i)cost[i]=oo;
Dijkstra(1);
for(int i=2;i<=n;++i)
if(cost[i]==oo)g<<0<<' ';
else g<<cost[i]<<' ';
f.close(); g.close();
return 0;
}