Pagini recente » Cod sursa (job #1713805) | Cod sursa (job #3257936) | Cod sursa (job #2859899) | Cod sursa (job #2486866) | Cod sursa (job #2365288)
#include <bits/stdc++.h>
using namespace std;
#define oo 1000000005 ///infinit
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
vector <pair<int,int>>v[50005]; ///liste de vecini
/// coada de prioritate - se memoreaza crescator valorile dupa cost (adica descrescator dupa -cost)
priority_queue<pair<int,int>> q;
int n,m,costv[50005],x,y,c;
int main()
{
fin>>n>>m;
for(int i=1;i<=m;++i)
{ fin>>x>>y>>c;
v[x].push_back(make_pair(y,c));
}
for(int i=2;i<=n;++i)costv[i]=oo;
q.push(make_pair(0,1)); /// adaug nodul de plecare 1 ce are cost 0
while(!q.empty())
{
int cost=-q.top().first;
int k=q.top().second;
q.pop();
/// merg pana ajung la un nod nevizitat de cost minim
if(cost!=costv[k]) continue;
///imbunatatesc costurile vecinilor nodului k
for(auto x: v[k])
if(costv[x.first]>costv[k]+x.second)
{
costv[x.first]=costv[k]+x.second;
q.push(make_pair(-costv[x.first],x.first));
}
}
for(int i=2;i<=n;++i)
if(costv[i]==oo)fout<<0<<" ";else fout<<costv[i]<<" ";
return 0;
}