Pagini recente » Cod sursa (job #2690881) | Cod sursa (job #2518519) | Cod sursa (job #1039510) | Cod sursa (job #2388227) | Cod sursa (job #2399003)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
#include <string>
#define Nmax 50001
#define inf 0x3f3f3f3f
using namespace std;
int main()
{
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int n, m, dist[Nmax], used[Nmax], x, y, c;
priority_queue<pair<int, int>, vector<pair< int, int>>, greater<pair< int, int >>> heap;
vector<pair<int, int>> graf[Nmax];
f >> n >> m;
while(m--)
{
f >> x >> y >> c;
graf[x].push_back({y, c});
}
for(int i = 1; i <= n; ++i)
{
dist[i] = inf;
used[i] = 0;
}
heap.push({0, 1});
dist[1] = 0;
used[1] = 1;
while(!heap.empty())
{
int node = heap.top().second;
heap.pop();
for(auto v : graf[node])
{
if(dist[node] + v.second < dist[v.first])
{
dist[v.first] = dist[node] + v.second;
if(!used[v.first])
{
heap.push({dist[v.first], v.first});
used[v.first] = 1;
}
}
}
}
for(int i = 2; i <= n; ++i)
if(dist[i] == inf)
g << 0 << ' ';
else
g << dist[i] << ' ';
}