Pagini recente » Cod sursa (job #1432677) | Cod sursa (job #1699704) | Cod sursa (job #2759059) | Cod sursa (job #320483) | Cod sursa (job #2549205)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
struct Vecin
{
int y, c;
Vecin(int _y, int _c)
{
y = _y, c = _c;
}
bool operator > (const Vecin& other) const
{
return c > other.c;
}
};
int n, m, D[50001];
vector<Vecin> G[50001];
bool V[50001];
void Dijkstra()
{
for (int i = 2; i <= n; ++i)
D[i] = 0x3f3f3f3f;
priority_queue<Vecin, vector<Vecin>, greater<Vecin>> Q;
Q.emplace(1, 0);
while (!Q.empty())
{
int x = Q.top().y, c = Q.top().c;
Q.pop();
if (c > D[x])
continue;
V[x] = true;
for (Vecin v : G[x])
{
if (!V[v.y] && D[x] + v.c < D[v.y])
{
Q.emplace(v.y, D[x] + v.c);
D[v.y] = D[x] + v.c;
}
}
}
}
int main()
{
fin >> n >> m;
for (int i = 1; i <= m; ++i)
{
int x, y, c;
fin >> x >> y >> c;
G[x].emplace_back(y, c);
}
Dijkstra();
for (int i = 2; i <= n; ++i)
{
if (D[i] == 0x3f3f3f3f)
fout << 0 << " ";
fout << D[i] << " ";
}
return 0;
}