Advertisement
VSS2

Untitled

May 13th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <array>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. typedef  pair<int,int> ruas;
  10.  
  11. vector<pair<int, ruas>> grafo;
  12.  
  13. int cruzamento[200001];
  14.  
  15. int buscador(int item){
  16.     if(item != cruzamento[item]){
  17.         cruzamento[item] = buscador(cruzamento[item]);
  18.     } else {
  19.         return cruzamento[item];
  20.     }
  21.     return cruzamento[item];
  22. }
  23.  
  24. int Kruskredo(int numruas){
  25.     int primeiro, segundo, distmin;
  26.    
  27.     sort(grafo.begin(), grafo.end());
  28.    
  29.     int pos = 0; int distmin = 0;
  30.    
  31.     while(pos < numruas){
  32.         primeiro = buscador(grafo[pos].second.first);
  33.         segundo = buscador(grafo[pos].second.second);
  34.        
  35.         if(primeiro != segundo){
  36.             cruzamento[segundo] = cruzamento[primeiro];
  37.             distmin -= grafo[pos].first;
  38.         }
  39.         pos++;
  40.     }
  41.     return distmin;
  42. }
  43.  
  44. int main(){
  45.    
  46.     int juncoes, numruas, comprimento, rua1, rua2;
  47.  
  48.     while(scanf("%i %i", &juncoes, &numruas)){
  49.  
  50.         if (juncoes == 0 && numruas == 0){
  51.             break;
  52.         } else {
  53.             distmax=0;
  54.            
  55.             int c = 0;
  56.  
  57.             for(c = 0; c < numruas; c++){
  58.                 scanf("%i %i %i", &rua1, &rua2, &comprimento);
  59.                 distmax += comprimento;
  60.                 grafo.push_back(pair<int, ruas>(comprimento, ruas(rua1, rua2)));
  61.             }
  62.  
  63.             int menor = Kruskredo(numruas);
  64.            
  65.             cout << (distmax + menor) << endl;
  66.            
  67.             grafo.clear();
  68.            
  69.                 while (juncoes-- > 0){
  70.                     cruzamento[juncoes] = 0;
  71.                 }
  72.             }
  73.         }
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement