Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<queue>
  4. using namespace std;
  5. int main(){
  6.     int v;
  7.     cin >> v;
  8.     vector<vector<pair<int,int>>> g(v);
  9.     for(int i=0; i<v; i++){
  10.         int times,to,edge;
  11.         cin >> times;
  12.         for(int j=0; j<times; j++){
  13.             cin >> to >> edge;
  14.             g[i].push_back(make_pair(to,edge));
  15.         }
  16.     }
  17.     int d[v];
  18.     for(int i=0; i<v; i++) d[i] = 40000;
  19.     priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
  20.     pq.push(make_pair(0,0));
  21.     while(!pq.empty()){
  22.         pair<int,int> f = pq.top();
  23.         pq.pop();
  24.         if(f.first < d[f.second]) d[f.second] = f.first;
  25.         for(auto it : g[f.second]) pq.push(make_pair(f.first+it.second,it.first));
  26.     }
  27.     for(int i=0; i<v; i++){
  28.         cout << d[i] << " ";
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement