Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- struct Edge{
- int to;
- ll weight;
- };
- struct Node{
- int node;
- ll weight;
- bool operator>(const Node &a) const{
- return weight>a.weight;
- }
- };
- ll INF = LLONG_MAX;
- int n, m;
- vector<vector<Edge>> con;
- priority_queue<Node, vector<Node>, greater<Node>> pq;
- int main(){
- //freopen("shortestroute.in", "r", stdin);
- cin >> n >> m;
- con.resize(n+1);
- vector<long long> dist(n+1, INF);
- for(int i=0; i<m; i++){
- int a, b, c;
- cin >> a >> b >> c;
- con[a].push_back({b, c});
- //con[b].push_back({a, c});
- }
- pq.push({1, 0});
- dist[1] = 0;
- while(!pq.empty()){
- Node cur = pq.top();
- pq.pop();
- int weight = cur.weight;
- int node = cur.node;
- if(weight>dist[node]) continue;
- for(Edge e: con[node]){
- if(dist[e.to]>dist[node]+e.weight){
- //cout << "HI" << '\n';
- dist[e.to] = dist[node]+e.weight;
- //cout << dist[e.to] << '\n';
- pq.push({e.to, dist[e.to]});
- }
- }
- }
- string s = "";
- for(int i=1; i<=n; i++){
- cout << s << dist[i];
- s = " ";
- }
- cout << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement