Pagini recente » Cod sursa (job #1259582) | Cod sursa (job #1355752) | Cod sursa (job #2073893) | Cod sursa (job #1349554) | Cod sursa (job #2981329)
#include <bits/stdc++.h>
#define NMAX 50008
#define INF 1e9 + 8
using namespace std;
ifstream fin ("ctc.in");
ofstream fout ("ctc.out");
struct Muchie
{
int vf, cost;
};
struct comparare
{
bool operator() (const Muchie & a, const Muchie & b)
{
return a.cost > b.cost;
}
};
int n, m, dp[NMAX];
vector <Muchie> G[NMAX];
priority_queue <Muchie, vector<Muchie>, comparare> H;
void Dijkstra(int start);
int main()
{
int x, y, z;
fin >> n >> m;
for (int i = 1; i <= m; i++)
{
fin >> x >> y >> z;
G[x].push_back({y, z});
}
for (int i = 0; i <= n; i++)
dp[i] = INF;
Dijkstra(1);
for (int i = 2; i <= n; i++)
{
if (dp[i] == INF)
dp[i] = 0;
fout << dp[i] << ' ';
}
return 0;
}
void Dijkstra(int start)
{
dp[start] = 0;
H.push({start, 0});
while (!H.empty())
{
int vf = H.top().vf;
int cost = H.top().cost;
H.pop();
if (cost > dp[vf])
continue;
for (auto el : G[vf])
{
if (dp[el.vf] > dp[vf] + el.cost)
{
dp[el.vf] = dp[vf] + el.cost;
H.push({el.vf, dp[el.vf]});
}
}
}
}