Pagini recente » Cod sursa (job #2550907) | Cod sursa (job #3156273) | Cod sursa (job #213261) | Cod sursa (job #535882) | Cod sursa (job #2871064)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
struct elem
{
int cost,nod;
bool operator < (const elem &aux)
const {
return aux.cost<cost;
}
};
priority_queue<elem>q;
int n,m;
vector<pair<int,int>>adiacenta[50001];
void citire()
{
f >> n >> m;
for(int i = 1;i<=m;i++)
{
int x,y,c;
f >> x >> y >> c;
adiacenta[x].push_back({y,c});
}
}
int dist[50001];
void init()
{
for(int i = 1;i<=n;i++)
dist[i] = (1<<30)-1;
}
void push_coada(int cost,int nod)
{
elem e ;
e.cost = cost;
e.nod = nod;
q.push(e);
}
void alg()
{
push_coada(0,1);
while(!q.empty())
{
int nod = q.top().nod;
int cost = q.top().cost;
q.pop();
for(auto x:adiacenta[nod])
{
int nod_urm = x.first;
int cost_urm = x.second;
if(dist[nod_urm]>cost+cost_urm)
{
dist[nod_urm] = cost+cost_urm;
q.push({dist[nod_urm],nod_urm});
}
}
}
}
int main()
{
citire();
init();
alg();
for(int i = 2;i<=n;i++)
{
if(dist[i]!=(1<<30)-1)
g << dist[i]<< " ";
else
g<<"0 ";
}
}