图问题:帮助寻找两个最广泛分离的节点之间的距离节点、广泛、距离、两个

2023-09-11 05:00:04 作者:撒娇小小怪

我通过previous年ACM程序设计竞赛问题的工作试图获得在解决图形问题的更好。

I'm working through previous years ACM Programming Competition problems trying to get better at solving Graph problems.

该一个我的工作,现在是我给出的无向图的节点,他们的邻居和距离的连接节点的边任意数量。我需要的是从海誓山盟(重量的距离,而不是由#节点外)的最远的两个节点之间的距离。

The one I'm working on now is I'm given an arbitrary number of undirected graph nodes, their neighbors and the distances for the edges connecting the nodes. What I NEED is the distance between the two farthest nodes from eachother (the weight distance, not by # of nodes away).

现在,我确实有Dijkstra算法形式为:

Now, I do have Dijkstra's algorithm in the form of:

// Dijkstra's Single-Source Algorithm
private int cheapest(double[] distances, boolean[] visited)
{
        int best = -1;
        for (int i = 0; i < size(); i++)
        {
                if (!visited[i] && ((best < 0) || (distances[i] < distances[best])))
                {
                        best = i;
                }
        }
        return best;
}

// Dijkstra's Continued
public double[] distancesFrom(int source)
{
        double[] result = new double[size()];
        java.util.Arrays.fill(result, Double.POSITIVE_INFINITY);
        result[source] = 0; // zero distance from itself
        boolean[] visited = new boolean[size()];
        for (int i = 0; i < size(); i++)
        {
                int node = cheapest(result, visited);
                visited[node] = true;
                for (int j = 0; j < size(); j++)
                {
                        result[j] = Math.min(result[j], result[node] + getCost(node, j));
                }

        }
        return result;
}

通过此实现,我可以给它一个特殊的节点,它会给我从该节点的所有距离的列表。所以,我可以抢在距离该列表中的最大距离,但我不能确定任何特定节点是两个最远的人在两端之一。

With this implementation I can give it a particular node and it will give me a list of all the distances from that node. So, I could grab the largest distance in that list of distances but I can't be sure that any particular node is one of the two furthest ones at either end.

因此​​,唯一的解决办法,我能想到的是,每一个节点上运行此Dijkstra算法,通过距离的每个返回的清单,并寻找最大距离。用尽每个节点返回它的距离的名单后,我应该有任意两个节点之间的最大距离的值(两个最广泛分开村与村之间的在路上的距离)。目前已经得到了一个更简单的方式来做到这一点,因为这似乎真的计算昂贵。这个问题说,可能有多达500个节点采样输入,所以我不希望它采取望而却步长。这是我应该怎么办呢?

So the only solution I can think of is to run this Dijkstra's algorithm on every node, go through each returned list of distances and looking for the largest distance. After exhausting each node returning it's list of distances I should have the value of the largest distance between any two nodes (the "road" distance between the two most widely seperated villages). There has got to be an easier way to do this because this seems really computationally expensive. The problem says that there could be sample inputs with up to 500 nodes so I wouldn't want it to take prohibitively long. Is this how I should do it?

下面是问题的一个样本输入:

Here is a sample input for the problem:

总节:5

边缘: 节点2 - 连接 - 节点4距离/重量25 节点2 - 连接 - 节点5距离/重量26 节点3 - 连接 - 节点4距离/重量16 节点1 - 连接 - 节点4距离/重量14

Edges: Nodes 2 - Connect - Node 4. Distance/Weight 25 Nodes 2 - Connect - Node 5. Distance/Weight 26 Nodes 3 - Connect - Node 4. Distance/Weight 16 Nodes 1 - Connect - Node 4. Distance/Weight 14

这个问题的答案样本输入67.千里。这是两种最广泛分离村与村之间的道路长度。

The answer to this sample input is "67 miles". Which is the length of the road between the two most widely separated villages.

所以我应该做我该怎么描述还是有一个更简单和少得多的计算成本的方法?

So should I do it how I described or is there a much simpler and much less computationally expensive way?

推荐答案

看起来你可以使用的:

弗洛伊德沃肖尔算法 在约翰逊的算法。

我不能给你对他们太多指导,但 - 我不是专家

I can't give you much guidance about them though - I'm no expert.