Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <climits>
  4. #include <cstdio>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. const int NO_EDGE = 32767;
  10. typedef vector<vector<int>> adj_list;
  11.  
  12. adj_list min_span_tree(const adj_list& graph) {
  13.     int vertex_count = graph.size();
  14.  
  15.     vector<int> used(vertex_count, 0);
  16.     vector<int> min_edge(vertex_count, INT_MAX);
  17.     vector<int> parent_of_vertex(vertex_count, -1);
  18.  
  19.     adj_list answer;
  20.     answer.resize(vertex_count, vector<int>());
  21.  
  22.     min_edge[0] = 0;
  23.     for (int i = 0; i < vertex_count; ++i) {
  24.         int v = -1;
  25.         for (int j = 0; j < vertex_count; ++j)
  26.             if (!used[j] && (v == -1 || min_edge[j] < min_edge[v]))
  27.                 v = j;
  28.  
  29.         if (v == -1)
  30.             exit(EXIT_FAILURE);
  31.  
  32.         used[v] = 1;
  33.         if (parent_of_vertex[v] != -1) {   
  34.             answer[parent_of_vertex[v]].push_back(v);
  35.             answer[v].push_back(parent_of_vertex[v]);
  36.         }
  37.  
  38.         for (int to = 0; to < vertex_count; ++to)
  39.             if ((graph[v][to] != NO_EDGE) && (graph[v][to] < min_edge[to])) {
  40.                 min_edge[to] = graph[v][to];
  41.                 parent_of_vertex[to] = v;
  42.             }
  43.     }
  44.  
  45.     return answer;
  46. }
  47.  
  48. void process_answer(adj_list& answer) {
  49.     int vertex_count = answer.size();
  50.  
  51.     for (int i = 0; i < vertex_count; ++i)
  52.         sort(answer[i].begin(), answer[i].end());
  53. }
  54.  
  55. int calculate_tree_weight(const adj_list& graph, const adj_list& answer) {
  56.     int vertex_count = answer.size();
  57.     int tree_weight = 0;
  58.    
  59.     for (int from = 0; from < vertex_count; ++from)
  60.         for (int index = 0; index < answer[from].size(); ++index) {
  61.                 int to = answer[from][index];
  62.                 tree_weight += graph[from][to];
  63.         }
  64.  
  65.     return tree_weight >> 1;
  66. }
  67.  
  68. int main() {
  69.     freopen("input.txt", "r", stdin);
  70.     freopen("output.txt", "w", stdout);
  71.  
  72.     int vertex_count;
  73.     adj_list graph;
  74.  
  75.     cin >> vertex_count;
  76.     graph.resize(vertex_count, vector<int>(vertex_count));
  77.  
  78.     for (int i = 0; i < vertex_count; ++i)
  79.         for (int j = 0; j < vertex_count; ++j)
  80.             cin >> graph[i][j];
  81.  
  82.     adj_list answer = min_span_tree(graph);
  83.     process_answer(answer);
  84.  
  85.     for (int from = 0; from < vertex_count; ++from) {
  86.         for (int index = 0; index < answer[from].size(); ++index)
  87.             cout << answer[from][index] + 1 << ' ';
  88.         cout << '0' << endl;
  89.     }
  90.     cout << calculate_tree_weight(graph, answer) << endl;
  91.  
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement