Pagini recente » Cod sursa (job #549343) | Cod sursa (job #1453408) | Cod sursa (job #2850128) | Cod sursa (job #1281759) | Cod sursa (job #2887021)
#include <bits/stdc++.h>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
const int lim=5e4+5;
const int inf=1e9+7;
vector<pair<int,int> > vec[lim];
struct stare
{
int cost;
int nod;
};
struct cmp
{
bool operator ()(const stare &x,const stare &y)
{return x.cost>y.cost;}
};
priority_queue<stare,vector<stare>,cmp> pq;
int dist[lim];
int n,m,x,y,c;
int main()
{
in>>n>>m;
for(int i=1;i<=m;++i)
in>>x>>y>>c,
vec[x].push_back({y,c});
for(int i=1;i<=n;++i)
dist[i]=inf;
dist[1]=0;
pq.push({0,1});
while(!pq.empty())
{
int cost=(pq.top()).cost;
int nod=(pq.top()).nod;
pq.pop();
if(cost!=dist[nod])
continue;
for(auto p:vec[nod])
if(dist[p.first]>cost+p.second)
{
dist[p.first]=cost+p.second;
pq.push({cost+p.second,p.first});
}
}
for(int i=2;i<=n;++i)
if(dist[i]==inf) out<<0<<' ';
else out<<dist[i]<<' ';
out<<'\n';
return 0;
}