AlbertMourato

Untitled

Mar 2nd, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <bits/stdc++.h>
  3. //%lld
  4. typedef long long ll;
  5. #define si(a) scanf("%d", &a)
  6. #define sii(a, b) scanf("%d%d", &a, &b)
  7. #define pi(a) printf("%d\n", a)
  8.  
  9. using namespace std;
  10. struct par{
  11.     int v;
  12.     int t;
  13. };
  14.  
  15. vector<par>grafo[100005];
  16. bool marcado[100005];
  17. ll custo[100005];
  18. int pai[100000000];
  19. int main(){
  20.     priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > fila;
  21.     int n, m, c, f, t, a, b;
  22.     int g = 1;
  23.     while(g--){
  24.         memset(marcado, false, sizeof(marcado));
  25.         memset(grafo, 0, sizeof(grafo));
  26.         fill(custo, custo+100005, 1000000000);
  27.         scanf("%d%d", &n, &m);
  28.         f = 0;
  29.         for(int i = 0;i<m;i++){
  30.             sii(a, b);
  31.             int peso;
  32.             si(peso);
  33.             par aux;
  34.             if(a>f)f = a;
  35.             aux.v= b;
  36.             aux.t = peso;
  37.             grafo[a].push_back(aux);
  38.             aux.v= a;
  39.             aux.t = peso;
  40.             grafo[b].push_back(aux);
  41.         }
  42.         fila.push(pair<int, int>(0, 1));//pair<indice, custo>
  43.         marcado[1] = true;
  44.         custo[1] = 0;
  45.         pai[1] = 1;
  46.         int sai = 0;
  47.         while(!fila.empty()){
  48.             pair<int, int> f = fila.top();
  49.             int auxx = f.first;
  50.             int aux = f.second;
  51.             fila.pop();
  52.             marcado[aux] = true;
  53.             for(int i = 0;i<(grafo[aux].size());i++){
  54.                 if(!marcado[grafo[aux][i].v]){
  55.                         if(custo[grafo[aux][i].v]>(grafo[aux][i].t)+auxx){
  56.                            custo[grafo[aux][i].v]=(grafo[aux][i].t)+auxx;
  57.                            fila.push(pair<int, int>(custo[grafo[aux][i].v],grafo[aux][i].v));
  58.                            pai[grafo[aux][i].v] = aux;
  59.                         }
  60.                 }
  61.             }
  62.         }
  63.         for(int i = 2;i<=f;i++){
  64.             sai+=custo[i]-custo[pai[i]];
  65.         }
  66.         cout<<sai<<endl;;
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment