Pagini recente » Cod sursa (job #2341707) | Cod sursa (job #1950527) | Cod sursa (job #1733452) | Cod sursa (job #2111611) | Cod sursa (job #2763171)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define FOR(i , n) for(int i = 0 ; i < (n) ; i++)
#define apare printf("apare");
#define endl "\n"
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int , int> pii;
////==================================================
const int inf = 1e6;
vector<int> Dijkstra(int node_count , vector<pii> adj[]){
vector<int> dist(node_count + 1 , inf);
vector<bool> processed(node_count + 1 , false);
dist[1] = 0;
priority_queue<pii> q;
q.push(make_pair(0 , 1));
while(!q.empty()){
int a = q.top().second;
q.pop();
if(processed[a]) continue;
processed[a] = true;
for(auto it : adj[a]) {
int b = it.second;
int w = it.first;
if (dist[a] + w < dist[b]) {
dist[b] = dist[a] + w;
q.push(make_pair(-dist[b], b));
}
}
}
return dist;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
freopen("dijkstra.in" , "r" , stdin);
freopen("dijkstra.out" , "w" , stdout);
int nodes , m;
cin >> nodes >> m;
vector<pii> adj[nodes + 1];
int node1 , node2 , weight;
FOR(i , m){
cin >> node1 >> node2 >> weight;
adj[node1].push_back(make_pair(weight , node2));
}
vector<int> ret = Dijkstra(nodes , adj);
for(int i = 2 ; i <= nodes ; i++){
if(ret[i] == inf)
cout << "0 ";
cout << ret[i] << " ";
}
return 0x0;
}