Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <set>
- #include <tuple>
- #include <unordered_map>
- using namespace std;
- using vertex_t = unsigned int;
- using cost_t = unsigned int;
- struct vertex_comporator {
- vector<vertex_t> convertion_back;
- bool operator() (const pair<vertex_t,cost_t> &u, const pair<vertex_t,cost_t> &v) {
- return ((u.second < v.second) ||
- (u.second == v.second && convertion_back[u.first] < convertion_back[v.first]));
- }
- vertex_comporator(vector<vertex_t> convertion_back) {
- this->convertion_back = convertion_back;
- }
- vertex_comporator() {}
- };
- unsigned int bvalue(unsigned int method, unsigned long node_id) {
- switch (method) {
- case 0: return 1;
- default: switch (node_id) {
- case 0: return 2;
- case 1: return 2;
- default: return 1;
- }
- }
- }
- unsigned int bvalue(unsigned int node_id);
- class Suitors {
- private:
- set<pair<vertex_t,cost_t>, vertex_comporator> adorate_them;
- vertex_comporator comporator;
- unsigned int size;
- public:
- Suitors(vector<vertex_t> converted_values, unsigned int size) {
- this->size = size;
- comporator = vertex_comporator(converted_values);
- adorate_them = set<pair<vertex_t,cost_t>, vertex_comporator>(comporator);
- }
- void insert(pair<vertex_t,cost_t> &v) {
- if (!last().second) adorate_them.insert(v);
- else {
- auto temp = adorate_them.begin();
- if (!comporator(*temp, v)) {
- adorate_them.erase(temp);
- adorate_them.insert(v);
- }
- }
- }
- pair<pair<vertex_t, cost_t>, bool> last() {
- if (adorate_them.size() < size) return make_pair(make_pair(0,0),false);
- return make_pair(*adorate_them.begin(), true);
- }
- };
- class Graph {
- private:
- vector<Suitors> suitors;
- vector<set<pair<vertex_t, cost_t>, vertex_comporator>> neighbours, adorators;
- vector<vertex_t> convertion;
- public:
- vector<tuple<vertex_t,vertex_t,cost_t>> _read_graph() {
- char c = 'a';
- vertex_t v1, v2;
- cost_t cost;
- vector<tuple<vertex_t,vertex_t,cost_t>> output_vector;
- while (c != EOF) {
- if (scanf("%c", &c) != 1) break;
- if (c == '#') {
- while (c != '\n' && c!= EOF) {
- if (scanf("%c", &c) != 1) c = EOF;
- }
- }
- else {
- ungetc(c, stdin);
- if (scanf("%u %u %u", &v1, &v2, &cost) != 3) continue;
- output_vector.push_back(make_tuple(v1,v2,cost));
- }
- }
- return output_vector;
- }
- unordered_map<vertex_t, vertex_t> _fill_conversion(vector<tuple<vertex_t,vertex_t,cost_t>> &triples) {
- unordered_map<vertex_t, vertex_t> map;
- vertex_t v1, v2;
- unsigned int i = 0;
- for (auto& elem : triples) {
- v1 = get<0>(elem);
- v2 = get<1>(elem);
- if (map.insert(make_pair(v1,i)).second) {
- convertion.push_back(v1);
- ++i;
- }
- if (map.insert(make_pair(v2, i)).second) {
- convertion.push_back(v2);
- ++i;
- }
- }
- return map;
- }
- void _fill_neighbours(vector<tuple<vertex_t,vertex_t,cost_t>> &triples,
- unordered_map<vertex_t, vertex_t> &map) {
- set<pair<vertex_t, cost_t>, vertex_comporator> s;
- vertex_comporator comporator = vertex_comporator(convertion);
- vertex_t v1, v2, source, target;
- cost_t cost;
- // set so that they are not empty
- for (unsigned int i = 0; i < convertion.size(); ++i) {
- s = set<pair<vertex_t, cost_t>, vertex_comporator>(comporator);
- neighbours.push_back(s);
- }
- for (auto& elem : triples) {
- v1 = get<0>(elem);
- v2 = get<1>(elem);
- cost = get<2>(elem);
- source = map.find(v1)->second;
- target = map.find(v2)->second;
- neighbours[source].insert(make_pair(target, cost));
- neighbours[target].insert(make_pair(source, cost));
- }
- for (auto& my_set : neighbours) {
- cout << "<<<<<<<<<" << endl;
- for (auto& elem : my_set) {
- cout << elem.first << " " << elem.second << endl;
- }
- cout << ">>>>>>>>>" << endl;
- }
- }
- /*
- void main_algorithm() {
- queue<vertex> q, r;
- vertex u;
- for (unsigned int i = 1; i < converted_index.size(); ++i) q.push(i);
- while (!q.empty()) {
- while (!q.empty()) {
- u = q.front();
- q.pop();
- }
- while (adorators[u].size() < bvalue(u)) {
- vector<vertex> diff;
- set_difference(neighbours[u].begin(), neighbours[u].end(),
- adorators[u].begin(), adorators[u].end(),
- inserter(diff, diff.begin()));
- vertex x = 0;
- for (vertex v : diff) {
- if (x == 0) x = v;
- else if (edges[u][v] > edges[v][suitors[v].last()]) x = v;
- }
- if (x == 0) break;
- else {
- vertex y = suitors[x].last();
- suitors[x].insert(u);
- adorators[u].push_back(x);
- if (y != 0) {
- adorators[y].reserve(x);
- r.push(y);
- }
- }
- }
- q = r;
- r = queue<vertex>();
- }
- }
- */
- };
- int main() {
- Graph g = Graph();
- auto a = g._read_graph();
- auto b = g._fill_conversion(a);
- g._fill_neighbours(a, b);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement