编程语言
首页 > 编程语言> > UVA10803 Thunder Mountain【Floyd算法】

UVA10803 Thunder Mountain【Floyd算法】

作者:互联网

“I mean, some people got guns, and some
people got flashlights, and some people got
batteries. These guys had all three.”
J. Michael Straczynski, "Jeremiah."

Markus is building an army to fight the evil Valhalla Sector, so he needs to move some supplies between several of the nearby towns. The woods are full of robbers and other unfriendly folk, so it’s dangerous to travel far. As Thunder Mountain’s head of security, Lee thinks that it is unsafe to carry supplies for more than 10km without visiting a town. Markus wants to know how far one would need to travel to get from one town to another in the worst case.

Input
The first line of input gives the number of cases, N. N test cases follow. Each one starts with a line containing n (the number of towns, 1 < n < 101). The next n lines will give the xy-locations of each town in km (integers in the range [0, 1023]). Assume that the Earth is flat and the whole 1024 × 1024 grid is covered by a forest with roads connecting each pair of towns that are no further than 10km away from each other.

Output
For each test case, output the line ‘Case #x:’, where x is the number of the test case. On the next line, print the maximum distance one has to travel from town A to town B (for some A and B). Round the answer to 4 decimal places. Every answer will obey the formula

|ans ∗ 10^4^ − ⌊ans ∗ 10^4^⌋ − 0.5| > 10^−2^
    If it is impossible to get from some town to some other town, print ‘Send Kurdy’ instead. Put an empty line after each test case.

Sample Input
2
5
0 0
10 0
10 10
13 10
13 14
2
0 0
10 1
Sample Output
Case #1:
25.0000
Case #2:
Send Kurdy

问题链接UVA10803 Thunder Mountain
问题简述:给定n个点,点到点距离大于10的不能走,求所有点到点距离中最大的距离,如果没有可以走则输出Send Kurdy。
问题分析:最短路问题,用Floyd算法解决。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10803 Thunder Mountain */

#include <bits/stdc++.h>

using namespace std;

/* Floyd-Warshall算法:计算图中任意2点之间的最短距离
 * 复杂度:O(N×N×N)
 * 输入:n 全局变量,图结点数
 *      g 全局变量,邻接矩阵,g[i][j]表示结点i到j间的边距离
 * 输出:g 全局变量
 */
const int INF = 0x3f3f3f3f;
const int N = 101;
double g[N][N];
int n;

void floyd()
{
    for(int k = 0; k < n; k++)
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
}

const double EPS = 1e-6;
struct Point {
    int x, y;
} p[N];

double getdis(int a, int b)
{
    return sqrt((p[a].x - p[b].x) * (p[a].x - p[b].x) + (p[a].y - p[b].y) * (p[a].y - p[b].y));
}

int main()
{
    int t;
    scanf("%d", &t);
    for(int k = 1; k <= t; k++) {
        scanf("%d", &n);
        for(int i = 0; i < n; i++) scanf("%d%d", &p[i].x, &p[i].y);

        for(int i = 0; i < n; i++)
            for(int j = i + 1; j < n; j++) {
                double dis = getdis(i, j);
                g[i][j] = g[j][i] = dis - 10.0 > EPS ? INF : dis;
            }

        floyd();

        double ans = 0;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                if(i != j) ans = max(ans, g[i][j]);

        printf("Case #%d:\n", k);
        if (fabs(ans - INF) < EPS) printf("Send Kurdy\n");
        else printf("%.4lf\n", ans);
        printf("\n");
    }

    return 0;
}

标签:town,10,int,Thunder,some,++,Floyd,ans,UVA10803
来源: https://blog.csdn.net/tigerisland45/article/details/114196648