Pagini recente » Cod sursa (job #2549650) | Cod sursa (job #1107455) | Cod sursa (job #507518) | Cod sursa (job #2761224) | Cod sursa (job #2698333)
#include <iostream>
#include <queue>
#include <fstream>
#include <vector>
using namespace std;
#define MAX_N 50005
const int INF = (1 << 30);
typedef pair<int,int> pereche;
vector<pereche> adj[MAX_N];
int dist[MAX_N], incoada[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});
incoada[1] = true;
while (!coada.empty())
{
x = coada.top().second;
coada.pop();
incoada[x] = false;
for (pereche i : adj[x])
{
y = i.second;
c = i.first;
if (!incoada[y] && dist[y] > dist[x] + c)
{
dist[y] = dist[x] + c;
coada.push({dist[y], y});
incoada[y] = true;
}
}
}
for (i = 2; i <= n; i++)
{
if (dist[i] != INF)
g << dist[i] << ' ';
else
g << 0 << ' ';
}
return 0;
}