首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >赋权图的邻接表表示法

赋权图的邻接表表示法
EN

Stack Overflow用户
提问于 2019-02-27 23:51:00
回答 1查看 1.3K关注 0票数 0

所以,伙计们,最近我一直在练习数据结构,图形等,我遇到了一个问题,与给定的代码。我正在实现一个图,它由一个邻接list.The问题表示,当我试图使用用户输入来实现解决方案时,我偶然遇到了这个问题。我尝试过很多方法,但我还是在程序中遇到了一些问题。因为我使用了一个接受整数列表的向量,所以每当我尝试用while循环填充向量时(例如),我不知道为什么,但我不能完全正确地填充它。因为我的错误是从那里开始的,所以我不知道如何继续这个程序。如果你们能给我一些关于如何实现我的代码来处理用户输入的提示,或者甚至给我一个类似的代码,我将不胜感激!

这是我的代码:

代码语言:javascript
复制
const int N = 4;

//purpose of the class is to tell what the weight of the given edge is
class Edge{
   private:
       double weight;
       int vertex_id;

   public:
    //constructor that initializes the weight and vertex id

    Edge(double w, int id)
    {
        weight = w;
       vertex_id = id;
    }

    double getWeight() const
        {
            return weight;
        }

    int getId() const
    {
        return vertex_id;
    }
};

int main()
{
    std::vector<std::list<Edge>> adjList(N); //creating our vector that will store a list of integers

    adjList[0].push_back(Edge(4,1)); //pushing back the first neighbours of our Zero list
    adjList[0].push_back(Edge(2,2)); //pushing back the second neighbours of our Zero list and so on...

    adjList[1].push_back(Edge(4,0));
    adjList[1].push_back(Edge(5,2));

    adjList[2].push_back(Edge(2,0));
    adjList[2].push_back(Edge(5,1));
    adjList[2].push_back(Edge(1,3));

    adjList[3].push_back(Edge(1,2));

    std::vector<std::list<Edge>>::iterator i; //declaring our vector iterator

    int c = 0; //we create a counter(and ofcourse assign zero to it)


    //create the for loop, where the iterator starts at the begining of the vector
    //and ends when the vector (adjList) ends


    //*KEEP IN MIND THAT THE FIRST FOR LOOP ONLY ITERATES THROUGH THE NODES OF THE VECTOR
    for (std::vector<std::list<Edge>>::iterator i = adjList.begin(); i != adjList.end(); i++)
    {

        cout << "Vertices connected to our node: " << c << endl;
        std::list<Edge> li = *i; //this pointer serves the purpose to get the list for each different node

        //NOW THE SECOND FOR LOOP ITERATES THROUGH THE LISTS, THAT EACH NODE CONTAINS
        for (std::list<Edge>::iterator iter = li.begin(); iter != li.end(); iter++)
        {
            cout << " (V = " << (*iter).getId() << " weight= " << (*iter).getWeight() <<")";
        }

        cout << endl; // we end the line between the different nodes of the vector

        c++; //increment our counter
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-28 00:03:38

一种简单的方法是预先询问边缘的数量,然后以from to weight格式读取许多以空格分隔的数字的三元组。这可以像下面这样简单:

代码语言:javascript
复制
int num_edges;
cin >> num_edges;
for (int i = 0; i < num_edges; i++) {
    int from, to, weight;
    cin >> from >> to >> weight;
    adjList[from].push_back(Edge(to, weight));
    // Uncomment this if you want an undirected graph
    // adjList[to].push_back(Edge(from, weight));
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54909372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档