Pagini recente » Cod sursa (job #1348482) | Cod sursa (job #2581801) | Cod sursa (job #1290471) | Cod sursa (job #3277695) | Cod sursa (job #3125878)
#include <fstream>
#include <queue>
#include <vector>
#define int long long
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
const int MAX = 5e4 + 1;
const int inf = 25e4*2e4+1;
int dp[MAX], n, m, x, y, c;
struct crit{
bool operator()( int a, int b ){
return dp[a] > dp[b];
}
};
priority_queue <int,vector<int>,crit> pq;
struct muchie{ int y, c;};
vector <muchie> g[MAX];
signed main(){
cin >> n >> m;
while(m--){
cin >> x >> y >> c;
g[x].push_back({y,c});
g[y].push_back({x,c});
}
for(int i = 2 ; i <= n ; i++) dp[i] = inf;
pq.push(1);
while(!pq.empty()){
x = pq.top();
pq.pop();
for(auto it : g[x]){
if(dp[it.y] > dp[x] + it.c){
dp[it.y] = dp[x] + it.c;
pq.push(it.y);
}
}
}
for(int i = 2 ; i <= n ; i++){
if(dp[i] == inf) dp[i] = 0;
cout << dp[i] << ' ';
}
return 0;
}