Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <vector>
- #include <array>
- #include <algorithm>
- using namespace std;
- typedef pair<int,int> ruas;
- vector<pair<int, ruas>> grafo;
- int cruzamento[200001];
- int buscador(int item){
- if(item != cruzamento[item]){
- cruzamento[item] = buscador(cruzamento[item]);
- } else {
- return cruzamento[item];
- }
- return cruzamento[item];
- }
- int Kruskredo(int numruas){
- int primeiro, segundo, distmin;
- sort(grafo.begin(), grafo.end());
- int pos = 0; int distmin = 0;
- while(pos < numruas){
- primeiro = buscador(grafo[pos].second.first);
- segundo = buscador(grafo[pos].second.second);
- if(primeiro != segundo){
- cruzamento[segundo] = cruzamento[primeiro];
- distmin -= grafo[pos].first;
- }
- pos++;
- }
- return distmin;
- }
- int main(){
- int juncoes, numruas, comprimento, rua1, rua2;
- while(scanf("%i %i", &juncoes, &numruas)){
- if (juncoes == 0 && numruas == 0){
- break;
- } else {
- distmax=0;
- int c = 0;
- for(c = 0; c < numruas; c++){
- scanf("%i %i %i", &rua1, &rua2, &comprimento);
- distmax += comprimento;
- grafo.push_back(pair<int, ruas>(comprimento, ruas(rua1, rua2)));
- }
- int menor = Kruskredo(numruas);
- cout << (distmax + menor) << endl;
- grafo.clear();
- while (juncoes-- > 0){
- cruzamento[juncoes] = 0;
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement