POJ 2263 Frogger(最大值的最小值) POJ 1797 Heavy Transportation (最小值的最大值)

Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists' sunscreen, he wants to avoid swimming and instead reach her by jumping.
Unfortunately Fiona's stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps.
To execute a given sequence of jumps, a frog's jump range obviously must be at least as long as the longest jump occuring in the sequence.
The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones.

You are given the coordinates of Freddy's stone, Fiona's stone and all other stones in the lake. Your job is to compute the frog distance between Freddy's and Fiona's stone.
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 Output
Scenario #1
Frog Distance = 5.000

Scenario #2
Frog Distance = 1.414
题目大意:n块石头,两只青蛙分别在第一块和第二块石头上,第一块石头上的青蛙想要找第二块石头的青蛙,可以借助其他石头跳过去,要求所有可能路径中的最大跳跃距离的最小值。(是不是有一点难理解啊)看例子: 比如三条路径,1 3 4;2 5,1 2 1;最大值分别为4,5,2,最小值就是2

思路:可以用迪杰斯克拉解决,也可以用弗洛伊德解决,只不过松弛的时候存的不再是最短路径而是最大跳跃距离中的最小值了。

坑点:输出结果要换两行,输出结果如果用%.3lf要用C++提交,不然会wa(一下子想不到吧!),用%.3f的话c++,g++无所谓。

迪杰斯克拉:

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
#define inf 0x3fffffff
int n;
double e[202][202],dis[202];
bool vis[202];
struct node
{
    double x,y;
}s[202];
double dist(node a,node b)
{
    return sqrt((double)((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)));
}
void dijkstra()
{
    memset(vis,false,sizeof(vis));
    for(int i=0;i<n;i++) dis[i]=e[0][i];
    for(int i=0;i<n-1;i++)
    {
        int u;
        double tmp=inf;
        for(int i=0;i<n;i++)
        {
            if(!vis[i]&&tmp>dis[i]&&dis[i])//因为要让青蛙尽可能跳地近,所以这里要选出最小值,跟下面一题刚好相反。
                tmp=dis[i],u=i;
        }
        vis[u]=true;
        for(int i=0;i<n;i++)
        {
            if((max(dis[u],e[u][i])<dis[i]||!dis[i])&&e[u][i]&&dis[u])//后面三个判断是因为e[i][j]中原来的值为0,要先判断他们是否为0,为0说明不存在路径
                dis[i]=max(dis[u],e[u][i]);//存最大跳跃距离中的最小值
        }
    }
}
int main()
{
    int k=1;
    while(~scanf("%d",&n)&&n)
    {
        for(int i=0;i<n;i++) scanf("%lf%lf",&s[i].x,&s[i].y);
        for(int i=0;i<n;i++)
            for(int j=i;j<n;j++)
                e[i][j]=e[j][i]=dist(s[i],s[j]);
        dijkstra();
        printf("Scenario #%d\nFrog Distance = %.3f\n\n",k++,dis[1]);
    }
}
弗洛伊德:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define inf 0x3fffffff
int n;
double e[202][202];
struct node
{
    double x,y;
}s[202];
double dist(node a,node b)
{
    return sqrt((double)((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)));
}
int main()
{
    int kk=1;
    while(~scanf("%d",&n)&&n)
    {
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&s[i].x,&s[i].y);
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                e[i][j]=e[j][i]=dist(s[i],s[j]);
        for(int k=0;k<n;k++)
            for(int i=0;i<n;i++)
                for(int j=0;j<n;j++)
                    if(e[k][i]&&e[k][j]&&max(e[k][i],e[k][j])<e[i][j])
                        e[i][j]=max(e[k][i],e[k][j]);//这里存的也不应该是最短路径了
        printf("Scenario #%d\nFrog Distance = %.3lf\n\n",kk++,e[0][1]);
    }
}
Background
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight.
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know.

Problem
You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo's place) to crossing n (the customer's place). You may assume that there is at least one path. All streets can be travelled in both directions.
Input
The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.
Output
The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for the scenario with a blank line.
Sample Input
1
3 3
1 2 3
1 3 4
2 3 5
Sample Output
Scenario #1:
4

题意:n个地点m条街道,每条街道都有载重量,要你求汽车从第一个地点到最后一个地点,汽车的最大重量是多少。

分析:要求汽车的最大重量,取决于这一条路上的街道的最小承载量,那么这题就是要求最小值中的最大值了。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define inf 0x3fffffff
int kkk,m,n,e[1005][1005],dis[1005];
bool vis[1005];
void dijkstra()
{
    memset(vis,false,sizeof(vis));
    for(int i=0;i<m;i++)
        dis[i]=e[0][i];
    for(int i=0;i<m-1;i++)
    {
        int tmp=0,u;
        for(int j=0;j<m;j++)
        {
            if(!vis[j]&&dis[j]&&tmp<dis[j])//因为要求汽车的最大重量,所以要选出一条最大的边来优化
                tmp=dis[j],u=j;
        }
        vis[u]=true;
        for(int j=0;j<m;j++)
        {
            //判断最小边是否大于当前路径上的承载量,如果大于则跟新
            if(e[u][j]&&(min(dis[u],e[u][j])>dis[j]||!dis[j]))
            {
                dis[j]=min(dis[u],e[u][j]);
            }
        }
    }
}
int main()
{
    int t,kk=1;
    scanf("%d",&t);
    while(t--)
    {
        memset(e,0,sizeof(e));//记得初始化地图
        int a,b,c;
        scanf("%d%d",&m,&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            e[a-1][b-1]=e[b-1][a-1]=c;
        }
        dijkstra();
        printf("Scenario #%d:\n%d\n\n",kk++,dis[m-1]);//每个答案之间要一个空行
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值