Pagini recente » Cod sursa (job #2377648) | Cod sursa (job #3182289) | Cod sursa (job #3189385) | Cod sursa (job #2788245) | Cod sursa (job #2698331)
#include <iostream>
#include <queue>
#include <fstream>
#include <vector>
#include <climits>
using namespace std;
#define MAX_N 50005
#define INF INT_MAX
typedef pair<int,int> pereche;
vector<pereche> adj[MAX_N];
int dist[MAX_N], viz[MAX_N];
int main()
{
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
priority_queue<pereche, vector<pereche>, greater<pereche>> coada;
int n, m, x, y, c, i;
f >> n >> m;
for (i = 1; i <= m; i++)
{
f >> x >> y >> c;
adj[x].push_back({c, y});
}
for (i = 1; i <= n; i++)
dist[i] = INF;
dist[1] = 0;
coada.push({0, 1});
while (!coada.empty())
{
x = coada.top().second;
coada.pop();
viz[x] = true;
for (pereche i : adj[x])
{
y = i.second;
c = i.first;
if (!viz[y] && dist[y] > dist[x] + c)
{
dist[y] = dist[x] + c;
coada.push({dist[y], y});
}
}
}
for (i = 2; i <= n; i++)
{
if (dist[i] != INF)
g << dist[i] << ' ';
else
g << 0 << ' ';
}
return 0;
}