Manioc

ilhas

May 17th, 2018
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define MAX 100007
  3.  
  4. using namespace std;
  5. typedef pair<int, int> pii;
  6. int n, m;
  7. int dist[MAX];
  8. vector<pii> graph[MAX];
  9.  
  10. void dijkstra(int no){
  11.     priority_queue<pii, vector<pii> > q;
  12.  
  13.     dist[no] = 0;
  14.     q.push(make_pair(0, no));
  15.     while(!q.empty()){
  16.         pii top = q.top();
  17.         q.pop();
  18.         int val = top.first;
  19.         int v = top.second;
  20.         //cout << val << " " << v << endl;
  21.         for(int i = 0; i < graph[v].size(); i++) {
  22.             int filho = graph[v][i].second;
  23.             int valor = graph[v][i].first;
  24.             //cout << "   " << valor << " " << filho << endl;
  25.             if(dist[filho] > (dist[v] + valor)){
  26.                 dist[filho] = dist[v] + valor;
  27.                 q.push(make_pair(dist[filho], filho));
  28.             }
  29.         }
  30.     }
  31.  
  32. }
  33. int main(){
  34.     scanf("%d %d", &n, &m);
  35.     for(int i = 0; i < m; i++){
  36.         int a, b, val;scanf("%d %d %d", &a, &b, &val);
  37.         graph[a].push_back(make_pair(val, b));
  38.         graph[b].push_back(make_pair(val, a));
  39.     }
  40.     for(int i = 0; i <= n; i++) dist[i] = 100000007;
  41.     int val; scanf("%d", &val);
  42.     dijkstra(val);
  43.     //for(int i = 1; i <= n; i++) cout << dist[i] << " ";
  44.     //cout << endl;
  45.  
  46.     int menor = 100000007, maior = 0;
  47.     for(int i = 1; i <= n; i++){
  48.         if(i != val){
  49.             menor = min(menor, dist[i]);
  50.             maior = max(maior, dist[i]);
  51.         }
  52.     }
  53.     printf("%d\n", maior-menor);
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment