稳定的婚姻算法

时间:2019-04-13 19:08:08

标签: c++

图形算法: 稳定的婚姻算法-在涉及人(姓,名,年龄)和偏好列表的基本数据的基础上生成“稳定”关系

我已经开始编写该程序,我想在加权直接图的帮助下编写该程序,并且在打印有关人员的数据时遇到问题,并且我不知道如何用它来填充列表。我不要求给我写代码,请给我一些想法 这是我的代码:

#include "pch.h"
#include <iostream>
#include <string>
#define N 6

using namespace std;

struct Graph {      
    struct Node* head[N];
};

struct Node {
    int age;
    string frst_nm;
    string lst_nm;
    int dest, weight;
    struct Node* next;
};

struct Edge {
    int src, dest, weight;

};

struct Inform
{
    int age;
    string first_name;
    string last_name;
};

struct Graph* createGraph(struct Edge edges[], struct Inform inf[], int n)
{
    unsigned i;
    int ag;
    string fr;
    string lst;

    struct Graph* graph = new Graph;

    /
    for (i = 0; i < N; i++)
        graph->head[i] = NULL;


    for (i = 0; i < n; i++) {

        int src = edges[i].src;
        int dest = edges[i].dest;
        int weight = edges[i].weight;
        int ag = inf[i].age;
        string fr = inf[i].first_name;
        string lst = inf[i].last_name; 

        struct Node* newNode = new Node;

        newNode->dest = dest;
        newNode->weight = weight;
        newNode->age = ag;
        newNode->frst_nm = fr;
        newNode->lst_nm = lst;

        newNode->next = graph->head[src];

        graph->head[src] = newNode;
    }

    return graph;
}

void printGraph(struct Graph* graph) {
    int i;
    for (i = 0; i < N; i++) {

        struct Node* ptr = graph->head[i];
        while (ptr != NULL) {
            cout << i << " : " << " " << ptr->age << " " << ptr->frst_nm << "  " << ptr->lst_nm << "   ";
            ptr = ptr->next;
        }

        cout << "\n";
    }
}

int main(void) {

    struct Edge edges[] = {
        { 0, 3, 3},{0,4,2}, { 0, 5, 1 }, { 1, 3, 1 }, { 1, 4, 3 },
        { 1, 5, 2 }, { 2, 3, 2 }, { 2, 4, 1 }, {2,5,3},{3,0,3},{3,1,1},{3,2,2},{4,0,3},{4,1,2}, {4,2,1},{5,0,1},{5,1,3},{5,2,2}
    };

    struct Inform inf[] = {
        {36, "John", "Smith"}, {25, "Edgar", "Po"}, {42, "Jim", "Kerry"}, {26, "Elise", "Cooper"},
    {39, "Merry", "Winston"}, {31, "Jess", "Bridget"}
    };

    int n = sizeof(edges) / sizeof(edges[0]);           

    struct Graph *graph = createGraph(edges,inf, n);

    printGraph(graph);

    return 0;
}

0 个答案:

没有答案