Pagini recente » Cod sursa (job #875609) | Cod sursa (job #1055718) | Cod sursa (job #2223533) | Cod sursa (job #39331) | Cod sursa (job #1918036)
#include <bits/stdc++.h>
using namespace std;
constexpr int maxn = 5e4 + 100;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
vector<pair<int, int>> vec[maxn] = {};
int n, m, dist[maxn] = {};
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>
pq;
void do_dijkstra(){
memset(dist, 0x3f, sizeof(dist));
dist[1] = 0;
pq.emplace(0, 1);
while(!pq.empty()){
int cur = pq.top().second, cur_dist = pq.top().first;
pq.pop();
if(dist[cur] != cur_dist) continue;
for(const auto next : vec[cur]){
if(cur_dist + next.first < dist[next.second]){
dist[next.second] = cur_dist + next.first;
pq.emplace(dist[next.second], next.second); } } } }
int main(){
f >> n >> m;
for(int i = 0, x, y, z; i < m; ++i){
f >> x >> y >> z;
vec[x].emplace_back(z, y);
vec[y].emplace_back(z, x); }
do_dijkstra();
transform(dist+2, dist+n+1, ostream_iterator<int>(g, " "),
[](const int x){ return x > 1e9 ? 0 : x; });
return 0; }