Pagini recente » Cod sursa (job #2316995) | Cod sursa (job #556046) | Cod sursa (job #2480048) | Cod sursa (job #1907557) | Cod sursa (job #2966490)
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 50100;
struct obj {
int first, second;
inline bool operator < (const obj &a) const {
return this->first < a.first;
}
};
int N, M;
vector <int> ans;
priority_queue <obj> heap;
vector <obj> edges[NMAX];
void read(){
scanf("%d%d", &N, &M);
int x, y, c;
for(int i = 1; i <= M; i++){
scanf("%d%d%d", &x, &y, &c);
edges[x].push_back({y, c});
}
ans.resize(N + 1, 2e9);
}
void solve(obj node){
heap.push(node);
ans[node.second] = 0;
while(!heap.empty()){
node = heap.top();
heap.pop();
if(ans[node.second] < node.first)
continue;
for(auto it : edges[node.second]){
if(ans[it.first] > ans[node.second] + it.second){
ans[it.first] = ans[node.second] + it.second;
heap.push({ans[node.second] + it.second, it.first});
}
}
}
}
int main() {
freopen("dijkstra.in", "r", stdin);
freopen("dijkstra.out", "w", stdout);
read();
solve({0, 1});
for(int i = 2; i <= N; i++){
if(ans[i] == 2e9)
ans[i] = 0;
printf("%d ", ans[i]);
}
return 0;
}