Pagini recente » Cod sursa (job #2467980) | Cod sursa (job #987744) | Cod sursa (job #2731883) | Monitorul de evaluare | Cod sursa (job #2114750)
#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;
bitset<N_MAX> viz; 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();
if(viz[nod]) continue;
viz[nod] = true;
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});
}
//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