English 中文(简体)
每一点问题之间最短的道路,即面粉战争
原标题:shortest path between all points problem, floyd warshall
月; 月;

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月

13月2 73月

49 86

72 111

<><>Output

For each test case, your program should output one line containing the minimum distance that Mr. Rowan must walk to visit all places assuming that the walking distance from one place to another is the Euclidean distance. The algorithm should output a number in fixed-point notation with exactly 3月 digits to the right of the decimal point and no leading space. There are at most 12 places to visit. Example

www.un.org/Depts/DGACM/index_spanish.htm 例:

3月

13月2 73月

49 86

72 111

<>光输出:

104.992

i 早就在为我的工作而努力制定这一法典,但我必须使其发挥作用,首先要问这是否最佳。

the problem is the floyd-warshall function, that does nothing on float **path structure.. dont know why.. path is the same before and after the 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("%.3月f	", path[i][j]);
            if (path[i][j] < mininum && path[i][j] != 0){
                mininum = path[i][j];
            }

        }
        printf("	minimum - %.3月f
", mininum);
        temp += mininum;
    }

    floydwarshall(path, n, next);


    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            printf("%.3月f	", next[i][j]);

        }
        printf("
");
    }

    /*
    temp = 0;
    for (i = 0; i < n; i++) {
        mininum = FLT_MAX;
        for (j = 0; j < n; j++) {
            printf("%.3月f	", path[i][j]);
            if (path[i][j] < mininum && path[i][j] != 0){
                mininum = path[i][j];
            }

        }
            printf("	minimum - %.3月f
", mininum);
        temp += mininum;
    }

    printf("%.3月f
", temp);

     */

    return 0;
}
月; 月;
问题回答

5. Floyd-Warshall 解决问题: 对于每个要点来说,找到最短的道路。 (需要加入这两个要点。) 它不需要做任何其他事情。 只有这样能够缩短路程,才会访问其他点。

在本案中,由于你总是能够直接从任何方面到任何其他方面,最短的道路总是直接的:从A到B。 (Which is 这就是为什么打电话floydwarshall。) 无改动。

但是,你试图解决的问题似乎是旅行销售商的问题:找到一条出访all<>>/em>的路程,并尽可能短。

这些都是完全不同的问题,你需要做一些非常不同的事情来解决你要求解决的问题。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签