Pagini recente » Cod sursa (job #3360142) | Cod sursa (job #3360303) | Monitorul de evaluare | Cod sursa (job #3359190) | Cod sursa (job #3360131)
#include <fstream>
#pragma GCC optimize("O3,unroll-loops")
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <iomanip>
#include <numeric>
#include <bitset>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#define int long long
//#define int short
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
const int nmax = 5e4;
const int inf = 2e9;
int n, m, rez[nmax + 5];
vector<pair<int, int>> adj[nmax + 5];
struct cmp{
bool operator()(const pair<int, int>& a, const pair<int, int>& b) const{
return a.second > b.second;
}
};
void dijsktra(int startnode){
priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> pq;
rez[startnode] = 0;
pq.push({startnode, 0});
while (!pq.empty()){
int curnode = pq.top().first, curweight = pq.top().second;
pq.pop();
if (rez[curnode] < curweight){
continue;
}
for (auto& [node, weight] : adj[curnode]){
int newweight = weight + curweight;
if (rez[node] > newweight){
rez[node] = newweight;
pq.push({node, newweight});
}
}
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n>>m;
while (m--){
int u, v, w;
cin>>u>>v>>w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
for (int i = 1; i <= n; i++){
rez[i] = inf;
}
dijsktra(1);
for (int i = 2; i <= n; i++){
cout<<rez[i]<<" ";
}
}