Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 功能列表应该是什么<;边缘>;返回?_List_Return Value_Dijkstra_Shortest Path - Fatal编程技术网

List 功能列表应该是什么<;边缘>;返回?

List 功能列表应该是什么<;边缘>;返回?,list,return-value,dijkstra,shortest-path,List,Return Value,Dijkstra,Shortest Path,现在我想做的是,对于从V1到V2的每条边,我想设置V2到V1的距离(D)。如果D小于当前到V2的距离,那么我们需要将V2的当前距离设置为D,并将V2的前置值设置为V1 我已经声明并初始化了V1到最短距离(这只是初始点),并将其标记为完成 问题:如何声明V2并设置其距离 std::list<Edge>* Graph::shortestPath(int fromVertex, int toVertex){ //initialize distance array set to IN

现在我想做的是,对于从V1到V2的每条边,我想设置V2到V1的距离(D)。如果D小于当前到V2的距离,那么我们需要将V2的当前距离设置为D,并将V2的前置值设置为V1

我已经声明并初始化了V1到最短距离(这只是初始点),并将其标记为完成

问题:如何声明V2并设置其距离

std::list<Edge>* Graph::shortestPath(int fromVertex, int toVertex){
    //initialize distance array set to INFINITY
    //initialize predecceor set to -1
    //initialize  bool done array to false

    std::list<Edge> *listOfEdges = new std::list<Edge>();
    std::list<Edge>::iterator it;
    Edge *edge;

    double *distance = new double [numVertices];
    int *predecessor = new int [numVertices];
    bool *done = new bool [numVertices];

    for(int i =0; i < numVertices; i++){
        distance[i] = INFINITY;
        predecessor[i] = -1;
        done[i] = false;
    }

    distance[fromVertex] = 0;
    predecessor[fromVertex] = UNDEFINED_PREDECESSOR;
    done[fromVertex] = true;


    for(int i =0; i < numVertices; i++){
        if(!done[i] && distance[i] != INFINITY){
            int V1 = getVertexWithSmallestDistanceThatsNotDone(distance, done);//choose smallest distance           
            done[V1] = true;//set vertice to to V1.


            double D = distance[toVertex] + distance[predecessor[toVertex]];
            if(D < distance[toVertex]){
                D = distance[toVertex];
                predecessor[toVertex] = fromVertex;
            }
        }
        return listOfEdges;
    }
}
std::list*图形::最短路径(int-fromVertex,int-to-Vertex){
//将距离数组设置为无穷大
//初始化PreDeceor设置为-1
//将布尔完成数组初始化为false
std::list*listOfEdges=new std::list();
std::list::迭代器;
边*边;
双精度*距离=新的双精度[数值];
int*前置=新int[numvities];
bool*done=新bool[numvities];
对于(int i=0;i
您正在返回指向std::list的指针。您通常会在函数中为此结果分配内存

std::list*result=new std::list()

然后,返回这个指针

返回结果

在获取此结果的外部函数中,需要释放动态分配的内存:

std::list<Edge>* edges = graph.shortestPath(1,5);

//work with edges

delete edges;
edges = NULL;//good practice to mark it as "not poiting to anything valid"
std::list*edges=graph.shortestPath(1,5);
//使用边缘
删除边缘;
边=空//将其标记为“不指向任何有效内容”的良好实践

谢谢。所以我创建了一个名为edges的列表并返回了它。我还没有为它设置值,因为我仍然不知道如何将值放入列表中。