您的位置:首页 > 其它

[kuangbin带你飞]专题四 最短路练习 B

2016-11-30 11:46 381 查看
真的不知道要败给读题到什么时候。。。

http://poj.org/problem?id=2253

Description

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.

题意:

从0到1,使经过的所有路径中长度最长的距离最短

咋就这么难懂。。。

tip:

看明白题倒是直接写出来了,看见有人问,也说一句吧。。。dij本身就是基于贪心的一种求最短路的方式,所谓贪心,就是保证每次松弛都是当前最优(短)的,那拿他去松弛别人保证了这些不会再松弛回来,所以有限。这道题的贪心最优变成了距离的最大值最小,还是个堆,只是现在的dis不是距离了,而是一条路上距离的最大值,使他最小。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <vector>
#include <deque>
#include <utility>
#include <functional>
const int maxn = 210;
const double eps = 1e-6;
const int INF = (1<<30);
using namespace std;
int n;
double ans;
double dis[maxn],dist[maxn][maxn];
typedef pair<int,double>pii;
priority_queue <pii,vector<pii> ,greater <pii> > pq;

void dij(){
while(!pq.empty()){
int no = pq.top().second;
if(no == 1) return;
pq.pop();
for(int i = 1 ; i < n ; i++){
if(dis[i] > max(dis[no],dist[i][no])){
dis[i] = max(dis[no],dist[i][no]);
pq.push(make_pair(dis[i],i));
}
}
}
}
int x[maxn],y[maxn];
void init(){
ans = 0;
for(int i = 0 ; i < n ; i++){
scanf("%d%d",&x[i],&y[i]);
}
for(int i = 1 ; i < n ; i++){
dis[i] = INF;
for(int j = i-1 ; j >= 0 ; j--){
dist[i][j] = dist[j][i] = sqrt((double)((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])));
//printf("dist[%d][%d] = %lf\n",i,j,dist[i][j]);
}
}

while(!pq.empty()){
pq.pop();
}
dis[0] = 0;
pq.push(make_pair(0,0));
}

int main(){
int ca = 0;
while(~scanf("%d",&n)&&n){
init();
dij();
printf("Scenario #%d\nFrog Distance = %.3f\n\n",++ca,dis[1]);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: