Pagini recente » Cod sursa (job #95191) | Cod sursa (job #2920015) | Cod sursa (job #2743711) | Cod sursa (job #1459732) | Cod sursa (job #2417022)
#include <bits/stdc++.h>
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout("dijkstra.out");
const int INF = 0x3f3f3f3f;
const int N_MAX = 50000 + 5;
int n, m;
vector<pii> vec[N_MAX];
int cost[N_MAX];
priority_queue<pii, vector<pii>, greater<pii> > q;
int main()
{
fin >> n >> m;
while(m--){
int aa, bb, cc;
fin >> aa >> bb >> cc;
vec[aa].push_back({bb,cc});
}
for(int i = 2; i <=n; ++i)
cost[i] = INF;
q.push({0, 1});
while(!q.empty()){
int nod = q.top().y;
int cst = q.top().x;
q.pop();
if(cost[nod] != cst)
continue;
for(auto v : vec[nod]){
int new_nod = v.x;
int add_cost = v.y;
if(cost[new_nod] > cst + add_cost){
cost[new_nod] = cst + add_cost;
q.push({cost[new_nod], new_nod});
}
}
}
for(int i = 2; i <= n; ++i)
if(cost[i] > INF)
fout << "0 ";
else
fout << cost[i] << " ";
return 0;
}