Advertisement
Guest User

Untitled

a guest
May 24th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <set>
  5. #include <algorithm>
  6. using namespace std;
  7. int main() {
  8.  
  9.     /*
  10.     Input:
  11.     [Numar Noduri]
  12.     [Nod1] [Nod2] [Pondere]
  13.     */
  14.     vector<pair<int, int> > graf;
  15.     int ponderi[10][10];
  16.    
  17.     vector<pair<int, int> > apcm;
  18.     int papcm[10][10];
  19.     ifstream f("Text.txt");
  20.     int n, a, b, c;
  21.     f >> n;
  22.     for (int i = 0; i < 10; i++) {
  23.         for (int j = 0; j < 10; j++) {
  24.             ponderi[i][j] = -1;
  25.             papcm[i][j] = -1;
  26.         }
  27.     }
  28.     while (f >> a >> b >> c) {
  29.         graf.push_back(make_pair(a-1, b-1));
  30.         ponderi[a - 1][b - 1] = c;
  31.         ponderi[b - 1][a - 1] = c;
  32.  
  33.     }
  34.  
  35.     vector<set<int> > sets(n);
  36.  
  37.     for (int i = 0; i <n;i++) {
  38.         sets[i].insert(i);
  39.     }
  40.  
  41.     //[&](auto fir, auto sec) {int i = ponderi[fir.first][fir.second], j = ponderi[sec.first][sec.second]; return i < j; }
  42.     //Asta e o expresie lambda, basically o functie anonima, se poate inlocui cu o functie ca la qsort de la C
  43.     sort(graf.begin(), graf.end(), [&](auto fir, auto sec) {int i = ponderi[fir.first][fir.second], j = ponderi[sec.first][sec.second]; return i < j; });
  44.     for (pair<int, int> a : graf) {
  45.         if (sets[a.first] != sets[a.second]) {
  46.             apcm.push_back(a);
  47.             set<int> _temp;
  48.             for (int i : sets[a.first]) _temp.insert(i);
  49.             for (int i : sets[a.second]) _temp.insert(i);
  50.             for(int a : _temp)
  51.                 sets[a] = _temp;
  52.             papcm[a.first][a.second] = papcm[a.second][a.first] = ponderi[a.second][a.first];
  53.         }
  54.     }
  55.  
  56.     for (pair<int, int> a : apcm) {
  57.         cout << a.first+1 << " " << a.second+1<< " " << papcm[a.first][a.second] << endl;
  58.     }
  59.  
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement