- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
Mr. Rowan plans to make a walking tour of Paris. However, since he is a little lazy, he wants to take the shortest path that goes through all the places he wants to visit. He plans to take a bus to the first place and another one back from the last place, so he is free to choose the starting and ending places. Can you help him?
Input
The first line of input contains the number of places to visit (n). Then, in the following n lines, you find the coordinates of each place to visit. Here is an example:
3
132 73
49 86
72 111
输出
对于每个测试用例,您的程序 应该输出一行包含 罗文先生必须的最小距离 步行参观所有地方假设 从一个地方到的步行距离 另一个是欧氏距离。这 算法应该输出一个数字 恰好为 3 的定点表示法 小数点右边的数字 点并且没有前导空格。有 最多参观12个地方。示例
示例输入:
3
132 73
49 86
72 111
示例输出:
104.992
我一直在编写这段代码,作为我的家庭作业,但我无法让它工作,我开始怀疑这是否是最好的方法..
问题出在 floyd-warshall 函数上,它对 float **path 结构没有任何作用。不知道为什么。在 floydwarshall(path, n, next); 前后路径相同
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <float.h>
/*Implementing of http://en.wikipedia.org/wiki/Floyd–Warshall_algorithm*/
struct point {
float x;
float y;
};
float cost(struct point* a, struct point* b) {
return sqrt(pow((*a).x - (*b).x, 2) + pow((*a).y - (*b).y, 2));
}
float** f2dmalloc(int n, int m){
int i;
float **ptr;
ptr = malloc(n * sizeof(float *));
for (i = 0; i < n; i++) {
ptr[i] = calloc(m, sizeof(float));
}
return ptr;
}
void floydwarshall(float **path, int n, float ** next){
int i, j, k;
float a, b;
for (k = 0; k < n; k++) {
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
a = path[i][j];
b = path[i][k] + path[k][j];
path[i][j] = ((a) < (b) ? a : b);
next[i][j] = k;
}
}
}
}
int main (int argc, const char* argv[])
{
int i;
int j;
int n;
float temp;
float mininum;
scanf("%d", &n);
/*
A 2-dimensional matrix. At each step in the algorithm, path[i][j] is the shortest path
from i to j using intermediate vertices (1..k−1). Each path[i][j] is initialized to
cost(i,j).
*/
float ** path;
float ** next;
struct point* points;
path = f2dmalloc(n, n);
next = f2dmalloc(n, n);
points = malloc(n * sizeof(struct point));
for (i = 0; i < n; i++){
scanf("%f %f", &(points[i].x), &(points[i].y));
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
path[i][j] = cost(&points[i], &points[j]);
}
}
temp = 0;
for (i = 0; i < n; i++) {
mininum = FLT_MAX;
for (j = 0; j < n; j++) {
printf("%.3f\t", path[i][j]);
if (path[i][j] < mininum && path[i][j] != 0){
mininum = path[i][j];
}
}
printf("\tminimum - %.3f\n", mininum);
temp += mininum;
}
floydwarshall(path, n, next);
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%.3f\t", next[i][j]);
}
printf("\n");
}
/*
temp = 0;
for (i = 0; i < n; i++) {
mininum = FLT_MAX;
for (j = 0; j < n; j++) {
printf("%.3f\t", path[i][j]);
if (path[i][j] < mininum && path[i][j] != 0){
mininum = path[i][j];
}
}
printf("\tminimum - %.3f\n", mininum);
temp += mininum;
}
printf("%.3f\n", temp);
*/
return 0;
}
最佳答案
Floyd-Warshall 解决了这个问题:对于每对点,找到连接它们的最短路径。 (它需要连接这两个点。它不需要做任何其他事情。如果产生更短的路径,它只会访问其他点。)
在目前的情况下,由于您始终可以从任何一点直接到达任何其他点,因此最短路径始终是直接路径:从 A 到 B。(这就是调用 floydwarshall
的原因不会改变任何东西。)
但是您要解决的问题似乎是旅行商问题:找到一条访问所有您的点并且尽可能短的路径。
这些是完全不同的问题,您需要做一些完全不同的事情来解决您被要求解决的问题。
关于c - 所有点之间的最短路径问题,floyd warshall,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5162885/
一 问题描述 求节点0到节点2的最短路径。 二 代码 package graph.floyd; import java.util.Scanner; public class Floyd {
一 背景介绍 如果求解任意两个节点之间的最短路径,则需要以每个节点为源点,重复调用 n 次 DijKstra 算法。其实是完全没有必要这么麻烦,Floyd 算法可用于求解任意两个节点之间的最短距离。F
我已经为一个 100 x 100 的邻接矩阵编写了代码,它表示以下有向图: 我正在尝试使用 Floyd-Warshall 算法为图中所有蓝色节点对找到最短路径。你如何只找到所选节点的所有对最短路径?这
我收到一个关于 Floyd's cycle-finding algorithm 的面试问题: Floyd 的循环查找算法何时会失败? 我的意思是,是否有规则可以找到快指针和慢指针之间的步长? 最佳答案
我正在尝试使用 java.awt.image.BufferedImage 在 Java 中实现 Floyd Steinberg 算法。 我使用了描述的算法 here使用自定义调色板,我希望获得与维基百
我正在尝试实现 Floyd-Warshall 算法(所有对最短路径)。在下面的代码中,当我输入一些数字时,它会给出最后一个数字作为输入。我知道代码不完整。 现在我应该怎么做才能为每个 i 和 j 打印
我试图在 .NET 的 C++ 上找到这个算法,但找不到,我找到了这个: // Best solution function boolean hasLoop(Node startNode){ No
我想在这个图问题中重建从源到目标顶点的路径。 如何存储路径,以及在找到从 s 到 d 的最小成本后如何检索它? 请帮我找到一个简单的答案? 例如在这一点上, adjmat[i][j] = Math.m
是否可以使用邻接表对 Floyd Warshall 进行编码?我必须处理文本文件中的一百万个顶点,因此邻接矩阵不是解决方案。任何实现已经可用?请帮忙。 最佳答案 您不能将 Floyd Warshall
我实现了 Floyd-Warshall 算法。根据他们的矩阵,我可以得到正确的结果,关于两个地方之间的最短路径和他们的距离。我的问题是如何打印从 i 到 j 的最短距离。我做了一些研究,发现了一个类似
这可能是一个糟糕的问题,因为我的代表很低,但我已经研究了几个小时的其他解决方案,我的代码似乎与我遇到的工作解决方案几乎相同。请不要忽略基于低代表的问题。 输出矩阵 d[][] 包含给定顶点对之间最短路
Mr. Rowan plans to make a walking tour of Paris. However, since he is a little lazy, he wants to tak
使用 Floyd 时如何使用我电脑上的数据文件? 运行后: floyd init floyd run --gpu --env tensorflow-1.3 "python model.py" 我得到的
假设我有 9 个顶点。所以我有 9x9 解决方案矩阵和 matrix[6,0] = infinity, matrix[6,9]=1, matrix[9,0]=1 现在算法的工作原理如下: for k
This维基百科页面解释了 Floyd Warshall 算法,用于查找图中节点之间的最短路径。维基百科页面使用图像左侧的图表 作为起始图(在 k = 0 时的第一次迭代之前),然后显示剩余的迭代(k
考虑以下链表: 1->2->3->4->5->6->7->8->9->4->...->9->4..... 上面的列表有一个循环如下: [4->5->6->7->8->9->4] 在白板上绘制链表,我尝
我知道当图中有负权重环时,没有找到最小距离的方法,也就没有最小距离的意义了。我的问题是,如果我们向 Floyd Warshall 算法提供具有负权重循环的图,会发生什么情况?它会在 O(n3) 内无限
有人可以告诉我这个过程在 for 迭代中的时间复杂度吗?这段代码是FloydWarshall算法的“重构路径”部分。prev[n][n]是最短路径中源节点和目的节点之间的节点矩阵。printAllSP
问题陈述:https://www.hackerrank.com/challenges/floyd-city-of-blinding-lights 代码: import scala.io.StdIn._
我能够理解 Floyd 循环查找算法工作原理的基本原理。我唯一无法理解的是 while 循环条件,如下所示: while(slow && fast && fast->next){ sl
我是一名优秀的程序员,十分优秀!