Pagini recente » Cod sursa (job #1816032) | Cod sursa (job #350160) | Cod sursa (job #1852250) | Cod sursa (job #1623538) | Cod sursa (job #2766218)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m;
struct elem
{
int nod, cost;
bool operator < (const elem other) const
{
return cost > other.cost;
}
};
vector<elem> v[50500];
priority_queue<elem> coada;
int ans[50500];
int main()
{
fin >> n >> m;
for(int i = 1; i <= n; i ++)
{
ans[i] = INT_MAX;
}
ans[1] = 0;
while(m--)
{
int u, w, c;
fin >> u >> w >> c;
v[u].push_back({w,c});
}
coada.push({1,0});
while(!coada.empty())
{
int nod = coada.top().nod;
int cost = coada.top().cost;
coada.pop();
if(cost == ans[nod])
{
for(int it = 0; it != v[nod].size(); it++)
{
if(cost + v[nod][it].cost < ans[v[nod][it].nod])
{
ans[v[nod][it].nod] = cost + v[nod][it].cost;
coada.push({v[nod][it].nod, ans[v[nod][it].nod]});
}
}
}
}
for(int i = 2; i <= n; i ++)
{
fout << ans[i] << ' ';
}
fout << '\n';
}