Pagini recente » Cod sursa (job #182110) | Cod sursa (job #584528) | Cod sursa (job #419620) | Cod sursa (job #2786719) | Cod sursa (job #2980990)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
string file = "dijkstra";
ifstream cin(file + ".in");
ofstream cout(file + ".out");
struct graf {
unsigned short nod;
int cost;
bool operator < (const graf& y) const
{
return cost > y.cost;
}
};
vector <graf> L[50001];
vector <int> d(50001);
void bfs()
{
priority_queue <graf> Q;
Q.push({ 1,0 });
while (!Q.empty())
{
unsigned short x = Q.top().nod;
Q.pop();
for (graf y : L[x])
{
if (d[y.nod] > d[x] + y.cost || d[y.nod] == 0)
{
d[y.nod] = d[x] + y.cost;
Q.push ({ y.nod,d[y.nod] });
}
}
}
}
int main() {
unsigned short n, x, y, z;
int m;
cin >> n >> m;
while (m--)
{
cin >> x >> y >> z;
L[x].push_back({ y,z });
}
bfs();
for (unsigned short i = 2; i <= n; i++)
cout << d[i] << ' ';
}