Pagini recente » Cod sursa (job #1797133) | Cod sursa (job #843711) | Cod sursa (job #3305935) | Cod sursa (job #852711) | Cod sursa (job #3304895)
#include <bits/stdc++.h>
#define int long long
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
struct elem
{
int nod, cost;
bool operator < (const elem & other) const
{
return cost>other.cost;
}
};
priority_queue <elem> pq;
vector <elem> v[50009];
int n, sol[50009];
void dijkstra ()
{
while (!pq.empty())
{
elem x=pq.top();
pq.pop();
if (x.cost==sol[x.nod])
{
for (auto y:v[x.nod])
{
if (x.cost+y.cost<sol[y.nod])
{
sol[y.nod]=x.cost+y.cost;
pq.push({y.nod, sol[y.nod]});
}
}
}
}
}
signed main ()
{
int m;
f >> n >> m;
for (int i=1; i<=m; i++)
{
int x, y, z;
f >> x >> y >> z;
v[x].push_back({y,z});
}
for (int i=2; i<=n; i++)
sol[i]=1e9;
pq.push({1, 0});
dijkstra ();
for (int i=2; i<=n; i++) g << sol[i] << ' ';
}