B - Frogger POJ - 2253
Posted dealer
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了B - Frogger POJ - 2253相关的知识,希望对你有一定的参考价值。
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
Output
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
初看似乎很难把这道题与单源最短路联系在一起。但我们可以做以下分析:
Dijkstra的贪心思想是基于这样一种前提:
设A→P1→P2→P3→B是A到B的最短路,则其中任意一部分亦是最短路。例如P1→P2→P3必然是P1到P3的最短路。证明很简单。假设P1到P3存在一条更短的路,则A到B便可通过该路获得一条更短的路,与题设“A→P1→P2→P3→B是A到B的最短路”矛盾。
由此便得到了Dijkstra的松弛方程:
枚举u。if (d[v]>d[u]+w(u,v)) then d[v]←d[u]+w(u,v)
其中d为源到点最短路长度。u为中转节点。w为u到v的最短路长度。
这是否能和本题的求解过程等同呢?
对于本题,易得两点间最长边最短的路径可能不唯一。但题目只关心点1到点2所有路径中最短的最长边的长度,并不关心具体路径。所以我们在求解过程中,可以人为定义所求路径满足以下条件:
设A→P1→P2→P3→B是我们想要求得的路径,则它满足其中任意一部分亦满足最长边最短。例如P1→P2→P3亦是所有P1到P3路径中最长边最短的。
这样一来,本题的求解,也满足了与Dijkstra相似的贪心过程。它的松弛方程为:
枚举j。if(dis[j]>max(dis[k],map[k][j])) then dis[j]=max(dis[k],map[k][j])
它的意思是,枚举所有中间点k,源到j的目标路径的最长边,要么在源到k这段,要么在k到j这段。
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
//#include <unordered_set>
//#include <unordered_map>
//#include <xfunctional>
#define ll long long
#define PII pair<int, int>
using namespace std;
int dir[5][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } ,{ 0,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979;
const int mod = 1e9 + 7;
const int maxn = 205;
//if(x<0 || x>=r || y<0 || y>=c)
//1000000000000000000
double x[maxn], y[maxn];
int sizedis = maxn*maxn;
double v[maxn][maxn];
vector<double> dis(sizedis, inf);
double dijkstra(int n)
{
for (int i = 1; i <= n; i++)
dis[i] = v[i][1];
dis[1] = 0;
vector<bool> vis(maxn, false);
for (int i = 1; i < n; i++)
{
int mini = 0;
double minn = inf;
for (int j = 1; j <= n; j++)
{
if (!vis[j] && dis[j] < minn)
{
mini = j;
minn = dis[j];
}
}
vis[mini] = true;
for (int j = 1; j <= n; j++)
{
if (!vis[j])
{
double mmax = max(minn, v[j][mini]);
if (mmax < dis[j])
dis[j] = mmax;
}
}
}
return dis[2];
}
int main()
{
int n,scen=0;
while (cin >> n && n != 0)
{
scen++;
cout << "Scenario #" << scen << endl;
for (int i = 1; i <= n; i++)
{
cin >> x[i];
cin >> y[i];
}
for (int i = 1; i <= n; i++)
{
for (int j = i + 1; j <= n; j++)
v[i][j] = v[j][i] = sqrt((x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j]));
}
cout << "Frog Distance = ";
printf("%.3f
", dijkstra(n));
}
return 0;
}
以上是关于B - Frogger POJ - 2253的主要内容,如果未能解决你的问题,请参考以下文章
POJ - 2253 Frogger(Floyd最短路+预处理)
POJ 2253 Frogger最短路变形/kruskal/A到B多条路径中的最小的最长边