Advertisement
Guest User

lab4

a guest
Apr 23rd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.82 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  comb_algs3
  4. //
  5. //  Created by Максим Бачурин on 27.03.2018.
  6. //  Copyright © 2018 Максим Бачурин. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <vector>
  11. #include <queue>
  12. #include <cmath>
  13. #include <numeric>
  14.  
  15. using namespace std;
  16.  
  17. const int inf = 1e9;
  18.  
  19. void kruskal() { //O(m log n + n^2)
  20.     int n, m;
  21.     cout << "Введите граф: " << endl;
  22.     cin >> n >> m;
  23.     vector<pair<int, pair<int, int>>> g(m); // вес, первая вершина, вторая
  24.     for(int i = 0; i < m; i++) {
  25.         int a, b, c;
  26.         cin >> a >> b >> c;
  27.         a--; b--;
  28.         g[i] = {c,{a,b}};
  29.     }
  30.     sort(g.begin(), g.end());
  31.     int cost = 0;
  32.     vector<pair<int, int>> result;
  33.     vector<int> tree_id(n);
  34.     for(int i = 0; i < n; i++) {
  35.         tree_id[i] = i;
  36.     }
  37.     for(int i = 0; i < m; i++) {
  38.         int a = g[i].second.first,
  39.         b = g[i].second.second,
  40.         l = g[i].first;
  41.         if(tree_id[a] != tree_id[b]) {
  42.             cost += l;
  43.             result.push_back({a, b});
  44.             int old_id = tree_id[b], new_id = tree_id[a];
  45.             for(int j = 0; j < n; j++) {
  46.                 if(tree_id[j] == old_id) {
  47.                     tree_id[j] = new_id;
  48.                 }
  49.             }
  50.         }
  51.     }
  52.     cout << "Минимальный остов: вес: " << cost << endl;
  53.     for(auto x : result) {
  54.         cout << x.first + 1 << " " << x.second + 1 << endl;
  55.     }
  56. }
  57.  
  58. pair<vector<int>,vector<pair<int, int>>> prim() { // O(n^2)
  59.     vector<pair<int, int>> ans;
  60.     int n, m;
  61.     cout << "Введите граф: " << endl;
  62.     cin >> n >> m;
  63.     vector<vector<int>> g(n);
  64.     for(int i = 0; i < n; i++) {
  65.         g[i].resize(n);
  66.         fill(g[i].begin(), g[i].end(), inf);
  67.     }
  68.     for(int i = 0; i < m; i++) {
  69.         int a, b, c;
  70.         cin >> a >> b >> c;
  71.         a--; b--;
  72.         g[a][b] = c;
  73.         g[b][a] = c;
  74.     }
  75.     vector<bool> used(n);
  76.     vector<int> min_e(n, inf), sel_e(n, -1), cost;
  77.     min_e[0] = 0;
  78.     for(int i = 0; i < n; i++) {
  79.         int v = -1;
  80.         for(int j = 0; j < n; j++) {
  81.             if(!used[j] && (v == -1 || min_e[j] < min_e[v]))
  82.                 v = j;
  83.         }
  84.         if (min_e[v] == inf) {
  85.             //cout << "Нет Остова";
  86.             return {{},{}};
  87.         }
  88.         used[v] = true;
  89.        
  90.         if (sel_e[v] != -1) {
  91.             //cout << v + 1 << " " << sel_e[v] + 1 << endl;
  92.             ans.push_back({v + 1, sel_e[v] + 1});
  93.             cost.push_back(min_e[v]);
  94.         }
  95.         for(int to = 0; to < n; to++) {
  96.             if(g[v][to] < min_e[to]) {
  97.                 min_e[to] = g[v][to];
  98.                 sel_e[to] = v;
  99.             }
  100.         }
  101.     }
  102.     //cout << "Вес: " << cost << endl;
  103.     return {cost, ans};
  104. }
  105.  
  106. void solve() {
  107.     auto ans = prim();
  108.     double p = 1;
  109.     for(auto x : ans.first) {
  110.         p *= (1 - (double)x/100);
  111.     }
  112.     p = 1 - p;
  113.     cout << "Лучшая вероятность: " << p << endl;
  114.     for(auto x : ans.second) {
  115.         cout << x.first << " " << x.second << endl;
  116.     }
  117. }
  118.  
  119. int main() {
  120.     //kruskal();
  121.    
  122. //    auto ans = prim();
  123. //    cout << "Вес: " << accumulate(ans.first.begin(), ans.first.end(), 0)
  124. //    << endl;
  125. //    for(auto x : ans.second) {
  126. //        cout << x.first << " " << x.second << endl;
  127. //    }
  128. //    
  129.     solve();
  130.     return 0;
  131. }
  132.  
  133. /*
  134.  4 4
  135.  1 2 10
  136.  2 3 10
  137.  3 4 10
  138.  4 1 11
  139.  
  140.  6 9
  141.  1 2 10
  142.  2 3 2
  143.  1 5 9
  144.  1 6 8
  145.  4 6 3
  146.  5 6 1
  147.  4 5 4
  148.  3 4 5
  149.  2 5 6
  150.  
  151.  12 24
  152.  1 2 6
  153.  2 3 15
  154.  3 9 6
  155.  1 5 5
  156.  2 5 8
  157.  2 4 18
  158.  1 4 11
  159.  7 8 9
  160.  8 9 14
  161.  7 9 4
  162.  9 10 19
  163.  7 10 13
  164.  8 12 5
  165.  11 12 7
  166.  6 11 3
  167.  5 6 15
  168.  4 6 10
  169.  2 7 11
  170.  3 8 18
  171.  4 11 17
  172.  4 7 7
  173.  5 7 9
  174.  7 11 12
  175.  10 12 2
  176.  
  177.  4 6
  178.  1 2 10
  179.  2 3 10
  180.  3 4 10
  181.  4 1 10
  182.  1 3 5
  183.  2 4 4
  184.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement