Pagini recente » Cod sursa (job #143728) | Cod sursa (job #2500525) | Cod sursa (job #2548069) | Cod sursa (job #2117576) | Cod sursa (job #2837804)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
#define local 69
std::ifstream fin("dijkstra.in");
std::ofstream fout("dijkstra.out");
#if !local
#define fin std::cin
#define fout std::cout
#endif
const int NMAX = 50000;
struct MUCHIE
{
int nod, cost;
};
bool operator<(const MUCHIE& a, const MUCHIE& b)
{
if (a.cost == b.cost)
{
return a.nod > b.nod;
}
return a.cost > b.cost;
}
std::vector <MUCHIE>G[NMAX + 5];
int n, viz[NMAX + 5], cost[NMAX + 5];
void Dijkstra(int start)
{
memset(cost, 0b01111111, sizeof(cost));
MUCHIE m;
std::priority_queue <MUCHIE> pq;
pq.push({start, 0});
while (!pq.empty())
{
while (!pq.empty() && viz[pq.top().nod])
{
pq.pop();
}
if (pq.empty())
{
break;
}
m = pq.top();
pq.pop();
viz[m.nod] = 1;
for (auto it : G[m.nod])
{
if (!viz[it.nod] && cost[it.nod] > m.cost + it.cost)
{
cost[it.nod] = m.cost + it.cost;
pq.push({it.nod, cost[it.nod]});
}
}
}
}
int main()
{
int m, x, y, z;
fin >> n >> m;
for (int i = 0; i < m; ++i)
{
fin >> x >> y >> z;
G[x].push_back({y, z});
}
Dijkstra(1);
for (int i = 2; i <= n; ++i)
{
if (cost[i] > 1 << 30)
{
fout << cost[i] << ' ';
}
else
{
fout << 0 << ' ';
}
}
return 0;
}