POJ - 2253 Frogger(路径最大值的最小值)

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

 

用最短路做(遍历所有可能的路径 所以不再求最短路):

修改松弛操作为:dis[i][j]=min(dis[i][j],max(dis[i][k]+dis[k][j]))

数据比较小,下面用的floyd。dijk也是一样。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
const int N=250;
const int inf=0x3f3f3f3f;

double g[N][N];
int x[N],y[N];
int n;

void floyd()
{
    for(int k=1;k<=n;k++)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
                g[i][j]=min(g[i][j],max(g[i][k],g[k][j]));///修改
        }
    }
}

int main()
{
    int kcase=0;
    while(scanf("%d",&n)!=EOF&&n)
    {
        memset(x,0,sizeof(x));
        memset(y,0,sizeof(y));
        memset(g,0,sizeof(g));
        scanf("%d%d",&x[1],&y[1]);
        scanf("%d%d",&x[n],&y[n]);
        for(int i=2;i<n;i++)
            scanf("%d%d",&x[i],&y[i]);
        getchar();
        for(int i=1;i<=n;i++)
        {
            g[i][i]=0;
            for(int j=i+1;j<=n;j++)
            {
                g[i][j]=g[j][i]=1.0*sqrt(1.0*(x[i]-x[j])*(x[i]-x[j])+1.0*(y[i]-y[j])*(y[i]-y[j]));
            }
        }
        floyd();
        printf("Scenario #%d\nFrog Distance = %.3f\n\n",++kcase,g[1][n]);
    }
    return 0;
}

dijkstra:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;

const int N = 250;
const double inf = 0x3f3f3f3f;
double g[N][N], dis[N];
bool vis[N];
int n;

struct node
{
    double x, y;
}s[N];

void dijk()
{
    memset(vis, 0, sizeof(vis));
    for(int i = 1; i <= n; i++)
        dis[i] = g[1][i];
    vis[1] = 1;
    dis[1] = 0;
    for(int i = 1; i <= n; i++)
    {
        int pos, minn = inf;
        for(int j = 1; j <= n; j++)
        {
            if(!vis[j] && minn > dis[j])
            {
                pos = j;
                minn = dis[j];
            }
        }
        vis[pos] = 1;
        for(int j = 1; j <= n; j++)
        {
            if(!vis[j] && dis[j] > max(dis[pos], g[pos][j]))
            {
                dis[j] = max(dis[pos], g[pos][j]);
            }
        }
    }
}

int main()
{
    double a, b;
    int kcase = 0;
    while(scanf("%d", &n) != EOF && n)
    {
        memset(s, 0, sizeof(s));
        for(int i = 1; i <= n; ++i)
        {
            scanf("%lf%lf", &a, &b);
            if(i == 1)
            {
                s[i].x = a;
                s[i].y = b;
            }
            else if(i == 2)
            {
                s[n].x = a;
                s[n].y = b;
            }
            else
            {
                s[i - 1].x = a;
                s[i - 1].y = b;
            }
        }
        for(int i = 1; i <= n; ++i)
        {
            for(int j = i; j <= n; ++j)
            {
                g[i][j] = g[j][i] = 1.0 * sqrt((s[i].x - s[j].x) * (s[i].x - s[j].x) + (s[i].y - s[j].y) * (s[i].y - s[j].y));
            }
        }
        dijk();
        cout<<"Scenario #"<<++kcase<<'\n';
        printf("Frog Distance = %.3f\n\n", dis[n]);
    }
    return 0;
}

 

用最小生成树做:

从最短边开始生成树,在两点联通的同时,结束生成树。

得到的就是这个不完整的生成树中的所有路径中的最大值的最小值(有点绕

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
const int N=45000;///边数组开大点
const int inf=0x3f3f3f3f;

struct node
{
    int u,v;
    double w;
    bool operator <(const node &a)const
    {
        return w<a.w;
    }
}edge[N];

int father[250];
int n,m;
int x[250],y[250];
double ans;

void init()
{
    for(int i=0;i<=n;i++)
        father[i]=i;
}

int Find(int x)
{
    if(x==father[x])
        return x;
    return father[x]=Find(father[x]);
}

void Union(int x,int y)
{
    int tmpx=Find(x);
    int tmpy=Find(y);
    if(tmpx!=tmpy)
    {
        father[tmpx]=tmpy;
    }
}


double kruskal()
{
    sort(edge,edge+m);
    init();
    node now;
    double ans=0;
    for(int i=0;i<m;i++)
    {
        now=edge[i];
        if(Find(now.u)!=Find(now.v))
        {
            Union(now.u,now.v);
            if(Find(1)==Find(n))
                return now.w;
        }
    }
}

int main()
{
    int kcase=0;
    while(scanf("%d",&n)!=EOF&&n)
    {
        memset(x,0,sizeof(x));
        memset(y,0,sizeof(y));
        scanf("%d%d",&x[1],&y[1]);
        scanf("%d%d",&x[n],&y[n]);
        for(int i=2;i<n;i++)
            scanf("%d%d",&x[i],&y[i]);
        getchar();
        m=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                edge[m].u=i;
                edge[m].v=j;
                edge[m].w=1.0*sqrt(1.0*(x[i]-x[j])*(x[i]-x[j])+1.0*(y[i]-y[j])*(y[i]-y[j]));
                m++;
            }
        }
        ans=kruskal();
        printf("Scenario #%d\nFrog Distance = %.3f\n\n",++kcase,ans);
    }
    return 0;
}

 

全部评论

相关推荐

03-06 20:09
贵州大学 Java
King987:你这个学历找个中大厂刷实习经历都是可以的,但是项目要有亮点才行,这个什么外卖就不要做了,去找找最新的项目,至少涉及高并发或者是新型的AI技术mcp rag啥的 ,我在出简历点评,但是你这个没什么好点评的,内容太少,而且含金量太低。自己改一改吧,或者看一下我的项目地址中,那里有大厂最近做过的实习项目
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
5015次浏览 47人参与
# 你的实习产出是真实的还是包装的? #
1103次浏览 27人参与
# MiniMax求职进展汇总 #
22891次浏览 293人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
6907次浏览 37人参与
# 简历第一个项目做什么 #
31251次浏览 312人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186349次浏览 1115人参与
# 米连集团26产品管培生项目 #
4127次浏览 198人参与
# 面试紧张时你会有什么表现? #
30371次浏览 188人参与
# 简历中的项目经历要怎么写? #
309379次浏览 4152人参与
# 网易游戏笔试 #
6304次浏览 83人参与
# 职能管理面试记录 #
10687次浏览 59人参与
# 把自己当AI,现在最消耗你token的问题是什么? #
6850次浏览 154人参与
# 从哪些方向判断这个offer值不值得去? #
56698次浏览 357人参与
# 腾讯音乐求职进展汇总 #
160394次浏览 1105人参与
# 小红书求职进展汇总 #
226845次浏览 1356人参与
# AI时代,哪些岗位最容易被淘汰 #
62406次浏览 728人参与
# 你怎么看待AI面试 #
179254次浏览 1163人参与
# 正在春招的你,也参与了去年秋招吗? #
362517次浏览 2631人参与
# 你的房租占工资的比例是多少? #
92123次浏览 896人参与
# 机械求职避坑tips #
94396次浏览 567人参与
# 校招笔试 #
466318次浏览 2950人参与
# 面试官最爱问的 AI 问题是...... #
27111次浏览 834人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务