Pagini recente » Cod sursa (job #72048) | Profil M@2Te4i | Rating Minzelevschi Igori (RainDropsMD) | Cod sursa (job #2012480) | Cod sursa (job #1999034)
#include <bits/stdc++.h>
#define nmax 50001
#define oo LONG_MAX
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n,m,d[nmax];
bool viz[nmax];
vector<pair<int,int > >L[nmax];
priority_queue<pair<int,int> >mn;
void Read()
{
int i,x,y,cost;
fin>>n>>m;
for(i=1;i<=m;i++)
{
fin>>x>>y>>cost;
L[x].push_back({y,cost});
}
}
void Solve()
{
int i,x,p,q;
for(i=2;i<=n;i++)
d[i]=oo;
mn.push({0,1});
while(!mn.empty())
{
x=mn.top().second;
mn.pop();
if(!viz[x])
{
viz[x]=true;
for(i=0;i<L[x].size();i++)
{
p=L[x][i].first;
q=L[x][i].second;
if(d[p]>d[x]+q)
{
d[p]=d[x]+q;
mn.push({-d[p],p});
}
}
}
}
for(i=2;i<=n;i++)
if(d[i]==oo)
fout<<"0 ";
else fout<<d[i]<<" ";
fout<<"\n";
}
int main()
{
Read();
Solve();
fin.close();
fout.close();
return 0;
}