Pagini recente » Cod sursa (job #1740445) | Cod sursa (job #3266863) | Cod sursa (job #1054720) | Cod sursa (job #646326) | Cod sursa (job #2329567)
#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 N_MAX = 50000 + 5;
const int INF = 0x3f3f3f3f;
int costs[N_MAX];
vector<pii> vec[N_MAX];
priority_queue<pii, vector<pii>, greater<pii> > q;
int n, m;
int main()
{
fin >> n >> m;
while(m--){
int a, b, c;
fin >> a >> b >> c;
vec[a].push_back({b,c});
}
for(int i = 1; i<=n; ++i)
costs[i] = INF;
costs[1] = 0;
q.push({0,1});
while(!q.empty()){
int cost = q.top().x;
int node = q.top().y;
q.pop();
if(cost != costs[node])
continue;
for(auto v : vec[node]){
int dist = v.y;
int vecNode = v.x;
if(costs[vecNode] > costs[node] + dist){
costs[vecNode] = costs[node] + dist;
q.push({costs[vecNode], vecNode});
}
}
}
for(int i = 2; i<=n; ++i)
if(costs[i] >= INF)
fout << "0 ";
else
fout << costs[i] << " ";
return 0;
}