功能列表应该是什么?返回?

发布于 2024-11-07 02:50:08 字数 1472 浏览 3 评论 0原文

现在我想做的是,对于从 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;
    }
}

Now what I want to do is, for each edge leading from V1 to V2, I want to set the distance(D) of V2 from V1. And if D is less than the current distant to V2 then we want to set V2's current distant to D and set V2's predecessor to V1.

I've declared and initialized V1 to the shortest distance (which is simply the initial point), and marked it as done.

Question: How do I declare a V2 and set it's distance?

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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

り繁华旳梦境 2024-11-14 02:50:08

您正在返回一个指向 std::list 的指针。您通常会在函数

std::list中为此结果分配内存。 *result = new std::list();

然后,您将返回此指针

return result

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

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"

You are returning a pointer to a std::list. You would normally allocate memory to this result in the function

std::list<Edge> *result = new std::list<Edge>();

Then, you would return this pointer

return result

In your outer function that grabs this result, you would need to free the memory that was dynamically allocated:

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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文