POJ 2253 Frogger (dijkstra 最大边最小)

Til the Cows Come Home

题目链接:

http://acm.hust.edu.cn/vjudge/contest/66569#problem/A

Description

The input will contain one or more test cases. The first line of each test case will contain the number of stones n (2<=n<=200). The next n lines each contain two integers xi,yi (0 <= xi,yi <= 1000) representing the coordinates of stone #i. Stone #1 is Freddy's stone, stone #2 is Fiona's stone, the other n-2 stones are unoccupied. There's a blank line following each test case. Input is terminated by a value of zero (0) for n.

Input

The input will contain one or more test cases. The first line of each test case will contain the number of stones n (2<=n<=200). The next n lines each contain two integers xi,yi (0 <= xi,yi <= 1000) representing the coordinates of stone #i. Stone #1 is Freddy's stone, stone #2 is Fiona's stone, the other n-2 stones are unoccupied. There's a blank line following each test case. Input is terminated by a value of zero (0) for n.

Output

For each test case, print a line saying "Scenario #x" and a line saying "Frog Distance = y" where x is replaced by the test case number (they are numbered from 1) and y is replaced by the appropriate real number, printed to three decimals. Put a blank line after each test case, even after the last one.

Sample Input

2
0 0
3 4

3
17 4
19 4
18 5

0

Sample Input

Scenario #1
Frog Distance = 5.000

Scenario #2
Frog Distance = 1.414

题意:

给出平面上的n个坐标,两两之间可联通;
求从#1到#2点的一条路径,使得其中最大的边最小;

题解:

直接用dijkstra实现即可;
dis[i]为起点s到当前点i的路径上最小的最大边;
本质与dijkstra求最短路一致;

POJ1797:求最小边最大;
(http://www.cnblogs.com/Sunshine-tcf/p/5693985.html)
本质一样,更新时存在区别;
另外,求最大最小边时,不能把dis[s]初始化为0(即循环n-1次),否则更新失败;

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define mid(a,b) ((a+b)>>1)
#define LL long long
#define maxn 250
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

int n;
double value[maxn][maxn];
double x[maxn],y[maxn];
double dis[maxn];
int pre[maxn];
bool vis[maxn];

void dijkstra(int s) {
    memset(vis, 0, sizeof(vis));
    memset(pre, -1, sizeof(pre));
    for(int i=1; i<=n; i++) dis[i] = inf;
    dis[s] = 0;

    for(int i=1; i<=n; i++) {
        int p;
        double mindis = inf;
        for(int j=1; j<=n; j++) {
            if(!vis[j] && dis[j]<mindis)
                mindis = dis[p=j];
        }
        vis[p] = 1;
        for(int j=1; j<=n; j++) {
            if(dis[j] > max(dis[p],value[p][j])) {
                dis[j] = max(dis[p], value[p][j]);
                pre[j] = p;
            }
        }
    }
}

int main(int argc, char const *argv[])
{
    //IN;

    int ca=1;
    while(scanf("%d", &n) != EOF && n)
    {
        memset(value, 0, sizeof(value));
        for(int i=1; i<=n; i++) scanf("%lf %lf", &x[i],&y[i]);
        for(int i=1; i<=n; i++) for(int j=i+1; j<=n; j++)
                value[i][j] = value[j][i] = sqrt((x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]));
        char tmp[10]; gets(tmp);

        dijkstra(1);

        printf("Scenario #%d
Frog Distance = %.3f

", ca++,dis[2]);
    }

    return 0;
}
原文地址:https://www.cnblogs.com/Sunshine-tcf/p/5693659.html