Pagini recente » Cod sursa (job #1578381) | Cod sursa (job #472523) | Cod sursa (job #1207875) | Cod sursa (job #2644993) | Cod sursa (job #2113838)
#define pii pair<int, int>
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout("dijkstra.out");
const int N_MAX = 50000 + 5;
const int inf = 0x3f3f3f3f;
int n, m, a, b, c;
int cost[N_MAX];
vector<pii> vec[N_MAX];
priority_queue<pii, vector<pii>, greater<pii> > q;
void dijkstra(){
cost[1] = 0;
q.push({0,1});
while(q.size()){
int nod = q.top().second;
q.pop();
for(auto v : vec[nod]){
int new_nod = v.first; int muchie_cost = v.second;
if(cost[nod] + muchie_cost < cost[new_nod]){
cost[new_nod] = cost[nod] + muchie_cost;
q.push({cost[new_nod], new_nod});
}
}
}
}
int main(){
fin >> n >> m;
while(m--){
fin >> a >> b >> c;
vec[a].push_back({b,c});
vec[b].push_back({a,c});
}
//init_cost();
memset(cost, inf, sizeof(cost));
dijkstra();
for(int i = 2; i<=n; ++i)
fout << (cost[i] == inf ? 0 : cost[i]) << " ";
return 0;
}
//Andrei Muntean, 2018