Pagini recente » Cod sursa (job #2271592) | Cod sursa (job #3000053) | Profil Djok | Cod sursa (job #2562993) | Cod sursa (job #3264829)
#include <bits/stdc++.h>
#define inf 0x3F3F3F3F
#define pi pair<int,int>
#define nmax 50005
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout("dijkstra.out");
int n,m,d[nmax];
bool viz[nmax];
vector<pi>L[nmax];
priority_queue<pi,vector<pi>,greater<pi>>pq;
int main()
{
fin>>n>>m;
while(m--)
{
int x,y,c;
fin>>x>>y>>c;
L[x].push_back(make_pair(y,c));
}
for(int i=1;i<=n;i++)
d[i]=inf;
d[1]=0;
pq.push({0,1});
while(!pq.empty())
{
int dist_curenta=pq.top().first;
int nod_curent=pq.top().second;
pq.pop();
if(viz[nod_curent]==1)
continue;
viz[nod_curent]=1;
for(auto i:L[nod_curent])
if(d[i.first]>d[nod_curent]+i.second)
{
d[i.first]=d[nod_curent]+i.second;
pq.push(make_pair(d[i.first],i.first));
}
}
for(int i=2;i<=n;i++)
{
if(d[i]==inf)
fout<<0<<' ';
else
fout<<d[i]<<' ';
}
return 0;
}