Pagini recente » Monitorul de evaluare | Borderou de evaluare (job #1212687) | Cod sursa (job #3211801) | Monitorul de evaluare | Cod sursa (job #3313639)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m, cost[50001], viz[50001];
vector <pair<int, int>> G[250001];
priority_queue<pair<int, int>> q;
void Dijkstra(int start)
{
int i, curr;
for (i = 1 ; i <= n ; i++)
cost[i] = 2e9;
cost[start] = 0;
q.push({0, start});
while (!q.empty())
{
curr = q.top().second;
q.pop();
if (viz[curr] == 0)
{
for (auto e : G[curr])
{
i = e.first;
if (cost[i] > cost[curr] + e.second)
{
cost[i] = cost[curr] + e.second;
q.push({-cost[i], i});
}
}
}
}
}
int main()
{
int i, x, y, c;
fin >> n >> m;
for (i = 1 ; i <= m ; i++)
{
fin >> x >> y >> c;
G[x].push_back({y, c});
}
Dijkstra(1);
for (i = 2 ; i <= n ; i++)
{
if (cost[i] == 2e9)
fout << 0 << " ";
else
fout << cost[i] << " ";
}
}