Pagini recente » Cod sursa (job #1971200) | Cod sursa (job #346245) | Cod sursa (job #2944526) | Cod sursa (job #648854) | Cod sursa (job #2459308)
#include <fstream>
#include <climits>
#include <queue>
#include <vector>
#define inf INT_MAX
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int viz[50001], i, x, y, n, m, d[50001], c, Min, j, node;
struct cmp{
bool operator() (int &lhs, int &rhs) const{
return d[lhs] > d[rhs];
}
};
vector < pair <int, int> > graph[50001];
priority_queue <int, vector <int>, cmp > heap;
int main()
{ f >> n >> m;
for(i = 1; i <= m; i++){
f >> x >> y >> c;
graph[x].push_back({y,c});
}
for(i = 2; i <= n; i++)
d[i] = inf;
heap.push(1);
while(!heap.empty()){
while(!heap.empty() && viz[heap.top()] == 1)
heap.pop();
node = heap.top();
for(j = 0; j < graph[node].size(); j++){
int new_node = graph[node][j].first;
int cost = graph[node][j].second;
if(d[new_node] > d[node] + cost && viz[new_node] == 0){
d[new_node] = d[node] + cost;
heap.push(new_node);
viz[new_node] = 0;
}
}
viz[node] = 1;
}
for(i = 2; i <= n; i++)
if(d[i] == inf)
g << 0 << ' ';
else
g << d[i] << ' ';
return 0;
}