Pagini recente » Cod sursa (job #3146700) | Cod sursa (job #724275) | Cod sursa (job #1500178) | Cod sursa (job #1426294) | Cod sursa (job #2175266)
#include <bits/stdc++.h>
#define Nmax 50005
using namespace std;
ifstream f ("dijkstra.in");
ofstream g ("dijkstra.out");
int d[Nmax],n,m,i,x,y,cost,nod,l,c,next_nod,INF = 0x3f3f3f3f;
set < pair < int,int > > heap;
vector < pair < int,int > > v[Nmax];
int main()
{
f >> n >> m;
for(i = 1; i <= m; i++)
{
f >> x >> y >> c;
v[x].push_back({y,c});
}
heap.insert({0,1});
memset(d, INF, sizeof(d));
d[1] = 0;
while(not heap.empty())
{
nod = heap.begin() -> second;
heap.erase(heap.begin());
for(i = 0,l = v[nod].size(); i < l; i++)
{
next_nod = v[nod][i].first;
cost = v[nod][i].second;
if(d[next_nod] > d[nod] + cost)
{
if(d[next_nod] != INF)
heap.erase(heap.find({d[next_nod],next_nod}));
d[next_nod] = d[nod] + cost;
heap.insert({d[next_nod],next_nod});
}
}
}
for(i = 2; i <= n; i++)
if(d[i] != INF)
g << d[i] << " ";
else g << 0 << " ";
return 0;
}