Description
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
题意:
从起点a到终点b,需利用几个点作为过渡点,从a到b,使得每一次跳跃的距离最小,求出这些跳跃值的最大值。
思路:
dijkstra算法,将其中求最短距离的数组d【】改为记录每次跳跃的最短距离。注意要在记录d[]的过程中一边更新ans值。
CODE:
#include
#include
#include
#include
#include
#include
using namespace std;
const double INF = 10000000.0;
const int MAXN = 205;
struct node { double x, y; } stone[MAXN];
struct edge { int to; double cost; };
typedef pair P;
int N;
vector g[250];
double d[MAXN], ans;
double dis(node a, node b)
{
return sqrt( (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) );
}
void dijkstra(int s)
{
ans = 0.0;
priority_queue, greater
> que;
fill(d, d + N + 1, INF);
d[s] = 0.0;
que.push(P(0.0, s));
while(!que.empty()) {
P p = que.top(); que.pop();
int v = p.second;
if(d[v] > p.first) continue;
if(d[v] != INF)
ans = max(ans, d[v]);
if(v == 2) return;
for(int i = 0; i < g[v].size(); ++i) {
edge e = g[v][i];
if(d[e.to] > e.cost) {
d[e.to] = e.cost;
que.push(P(d[e.to], e.to));
}
}
}
}
int main()
{
//freopen("in", "r", stdin);
int cas = 0;
while(~scanf("%d", &N)) {
if(N == 0) break;
cas ++;
for(int i = 0; i <= N; ++i) {
g[i].clear();
}
edge e;
for(int i = 1; i <= N; ++i) {
scanf("%lf %lf", &stone[i].x, &stone[i].y);
for(int j = 1; j < i; ++j) {
if(i == j) continue;
e.cost = dis(stone[i], stone[j]);
e.to = j;
g[i].push_back(e);
e.to = i;
g[j].push_back(e);
}
}
if(N == 2) {
double ans = dis(stone[1], stone[2]);
printf("Scenario #%d\nFrog Distance = %.3f\n\n", cas, ans);
continue;
}
dijkstra(1);
printf("Scenario #%d\nFrog Distance = %.3f\n\n", cas, ans);
}
return 0;
}