Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <tuple>
  5. #include <unordered_map>
  6.  
  7. using namespace std;
  8.  
  9. using vertex_t = unsigned int;
  10. using cost_t = unsigned int;
  11.  
  12. struct vertex_comporator {
  13.     vector<vertex_t> convertion_back;
  14.  
  15.     bool operator() (const pair<vertex_t,cost_t> &u, const pair<vertex_t,cost_t> &v) {
  16.         return ((u.second < v.second) ||
  17.                 (u.second == v.second && convertion_back[u.first] < convertion_back[v.first]));
  18.     }
  19.     vertex_comporator(vector<vertex_t> convertion_back) {
  20.         this->convertion_back = convertion_back;
  21.     }
  22.     vertex_comporator() {}
  23. };
  24.  
  25. unsigned int bvalue(unsigned int method, unsigned long node_id) {
  26.  
  27.     switch (method) {
  28.  
  29.         case 0: return 1;
  30.  
  31.         default: switch (node_id) {
  32.  
  33.                 case 0: return 2;
  34.  
  35.                 case 1: return 2;
  36.  
  37.                 default: return 1;
  38.  
  39.             }
  40.  
  41.     }
  42.  
  43. }
  44. unsigned int bvalue(unsigned int node_id);
  45.  
  46. class Suitors {
  47. private:
  48.     set<pair<vertex_t,cost_t>, vertex_comporator> adorate_them;
  49.     vertex_comporator comporator;
  50.     unsigned int size;
  51. public:
  52.     Suitors(vector<vertex_t> converted_values, unsigned int size) {
  53.         this->size = size;
  54.         comporator = vertex_comporator(converted_values);
  55.         adorate_them = set<pair<vertex_t,cost_t>, vertex_comporator>(comporator);
  56.     }
  57.  
  58.     void insert(pair<vertex_t,cost_t> &v) {
  59.         if (!last().second) adorate_them.insert(v);
  60.         else {
  61.             auto temp = adorate_them.begin();
  62.             if (!comporator(*temp, v)) {
  63.                 adorate_them.erase(temp);
  64.                 adorate_them.insert(v);
  65.             }
  66.         }
  67.     }
  68.  
  69.     pair<pair<vertex_t, cost_t>, bool> last() {
  70.         if (adorate_them.size() < size) return make_pair(make_pair(0,0),false);
  71.         return make_pair(*adorate_them.begin(), true);
  72.     }
  73.  
  74. };
  75.  
  76. class Graph {
  77. private:
  78.     vector<Suitors> suitors;
  79.     vector<set<pair<vertex_t, cost_t>, vertex_comporator>> neighbours, adorators;
  80.     vector<vertex_t> convertion;
  81. public:
  82.  
  83.     vector<tuple<vertex_t,vertex_t,cost_t>> _read_graph() {
  84.         char c = 'a';
  85.         vertex_t v1, v2;
  86.         cost_t cost;
  87.         vector<tuple<vertex_t,vertex_t,cost_t>> output_vector;
  88.         while (c != EOF) {
  89.             if (scanf("%c", &c) != 1) break;
  90.             if (c == '#') {
  91.                 while (c != '\n' && c!= EOF) {
  92.                     if (scanf("%c", &c) != 1) c = EOF;
  93.                 }
  94.             }
  95.             else {
  96.                 ungetc(c, stdin);
  97.                 if (scanf("%u %u %u", &v1, &v2, &cost) != 3) continue;
  98.                 output_vector.push_back(make_tuple(v1,v2,cost));
  99.             }
  100.         }
  101.         return output_vector;
  102.     }
  103.  
  104.     unordered_map<vertex_t, vertex_t> _fill_conversion(vector<tuple<vertex_t,vertex_t,cost_t>> &triples) {
  105.         unordered_map<vertex_t, vertex_t> map;
  106.         vertex_t v1, v2;
  107.         unsigned int i = 0;
  108.  
  109.         for (auto& elem : triples) {
  110.             v1 = get<0>(elem);
  111.             v2 = get<1>(elem);
  112.  
  113.             if (map.insert(make_pair(v1,i)).second) {
  114.                 convertion.push_back(v1);
  115.                 ++i;
  116.             }
  117.             if (map.insert(make_pair(v2, i)).second) {
  118.                 convertion.push_back(v2);
  119.                 ++i;
  120.             }
  121.         }
  122.         return map;
  123.     }
  124.  
  125.     void _fill_neighbours(vector<tuple<vertex_t,vertex_t,cost_t>> &triples,
  126.                           unordered_map<vertex_t, vertex_t> &map) {
  127.         set<pair<vertex_t, cost_t>, vertex_comporator> s;
  128.         vertex_comporator comporator = vertex_comporator(convertion);
  129.         vertex_t v1, v2, source, target;
  130.         cost_t cost;
  131.  
  132.         // set so that they are not empty
  133.         for (unsigned int i = 0; i < convertion.size(); ++i) {
  134.             s = set<pair<vertex_t, cost_t>, vertex_comporator>(comporator);
  135.             neighbours.push_back(s);
  136.         }
  137.  
  138.         for (auto& elem : triples) {
  139.             v1 = get<0>(elem);
  140.             v2 = get<1>(elem);
  141.             cost = get<2>(elem);
  142.             source = map.find(v1)->second;
  143.             target = map.find(v2)->second;
  144.             neighbours[source].insert(make_pair(target, cost));
  145.             neighbours[target].insert(make_pair(source, cost));
  146.         }
  147.  
  148.         for (auto& my_set : neighbours) {
  149.             cout << "<<<<<<<<<" << endl;
  150.             for (auto& elem : my_set) {
  151.                 cout << elem.first << " " << elem.second << endl;
  152.             }
  153.             cout << ">>>>>>>>>" << endl;
  154.         }
  155.     }
  156.  
  157.  
  158.  
  159.     /*
  160.     void main_algorithm() {
  161.         queue<vertex> q, r;
  162.         vertex u;
  163.         for (unsigned int i = 1; i < converted_index.size(); ++i) q.push(i);
  164.         while (!q.empty()) {
  165.             while (!q.empty()) {
  166.                 u = q.front();
  167.                 q.pop();
  168.             }
  169.             while (adorators[u].size() < bvalue(u)) {
  170.                 vector<vertex> diff;
  171.  
  172.                 set_difference(neighbours[u].begin(), neighbours[u].end(),
  173.                                adorators[u].begin(), adorators[u].end(),
  174.                                inserter(diff, diff.begin()));
  175.  
  176.                 vertex x = 0;
  177.                 for (vertex v : diff) {
  178.                     if (x == 0) x = v;
  179.                     else if (edges[u][v] > edges[v][suitors[v].last()]) x = v;
  180.                 }
  181.  
  182.                 if (x == 0) break;
  183.                 else {
  184.                     vertex y = suitors[x].last();
  185.                     suitors[x].insert(u);
  186.                     adorators[u].push_back(x);
  187.  
  188.                     if (y != 0) {
  189.                         adorators[y].reserve(x);
  190.                         r.push(y);
  191.                     }
  192.                 }
  193.             }
  194.             q = r;
  195.             r = queue<vertex>();
  196.         }
  197.     }
  198.      */
  199. };
  200.  
  201. int main() {
  202.     Graph g = Graph();
  203.     auto a = g._read_graph();
  204.     auto b = g._fill_conversion(a);
  205.     g._fill_neighbours(a, b);
  206.     return 0;
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement