题意:有两只青蛙,a在第一个石头,b在第二个石头,a要到b那里去,每种a到b的路径中都有最大边,求所有这些最大边的最小值。
思路:将所有边长存起来,排好序后,二分枚举答案。

  时间复杂度比较高,344ms。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h> using namespace std;
const int maxn=;
const int INF=0x3f3f3f3f;
double w[maxn][maxn]; //存储边长
double wlen[];
int con[maxn][maxn]; //con[i][j]=1表示i、j连通,con[i][j]=0表示不连通
int idx;
int n;
struct Node{
int x,y;
}node[maxn];
int main()
{
int t=,a,b;
double length;
int ans;
while(scanf("%d",&n)!=EOF){
if(n==)
break;
t++;
idx=;
memset(w,,sizeof(w));
printf("Scenario #%d\n",t);
for(int i=;i<n;i++){
scanf("%d%d",&a,&b);
node[i].x=a;
node[i].y=b;
}
for(int i=;i<n;i++){
for(int j=i+;j<n;j++){
//pow传递的参数先要强制转换成double,否则提交编译错误
length=pow(double(node[j].x-node[i].x),)+pow(double(node[j].y-node[i].y),);
length=sqrt(length);
w[i][j]=w[j][i]=length;
wlen[idx++]=length;
}
}
sort(wlen,wlen+idx);
//二分枚举所有可能的值,floyd的时候考虑所有长度不大于该值的边
int l=,r=idx-,mid;
while(l<=r){
mid=(l+r)>>;
for(int i=;i<n;i++){
for(int j=i+;j<n;j++){
//初始化,con[i][j]=1表示边i、j长度不大于枚举值,=0表示大于枚举值
if(w[i][j]>wlen[mid])
con[i][j]=con[j][i]=;
else
con[i][j]=con[j][i]=;
}
}
for(int k=;k<n;k++){
for(int i=;i<n;i++){
for(int j=;j<n;j++){
//只要有一对con[i][k]、con[k][j]连通,con[i][j]就连通
con[i][j]|=con[i][k]&con[k][j];
}
}
}
if(con[][]){
r=mid-;
ans=mid;
}
else{
l=mid+;
}
} printf("Frog Distance = %.3lf\n",wlen[ans]);
puts("");
}
return ;
}

这里附上别人的代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
double dist[][]; //dist[i][j]表示i到j的路径中边的最大值的最小值
int n;
struct Node {
int x;
int y;
} e[];
void Floyd() {
for(int k=; k<n; k++) {
for(int i=; i<n; i++) {
for(int j=; j<n; j++) {
if(max(dist[i][k],dist[k][j])<dist[i][j])
dist[i][j]=max(dist[i][k],dist[k][j]);
}
}
} }
int main() {
int t=;
while(~scanf("%d",&n)) {
if(n==)break;
for(int i=; i<n; i++) {
scanf("%d%d",&e[i].x,&e[i].y);
}
for(int i=; i<n; i++) {
for(int j=; j<n; j++) {
dist[i][j]=sqrt(pow((double)(e[i].x-e[j].x),)+pow((double)(e[i].y-e[j].y),));
}
}
Floyd();
printf("Scenario #%d\n",t++);
printf("Frog Distance = %.3f\n\n",dist[][]); }
}

POJ 2253 Frogger (求某两点之间所有路径中最大边的最小值)的更多相关文章

  1. POJ 2253 Frogger ,poj3660Cow Contest(判断绝对顺序)(最短路,floyed)

    POJ 2253 Frogger题目意思就是求所有路径中最大路径中的最小值. #include<iostream> #include<cstdio> #include<s ...

  2. POJ. 2253 Frogger (Dijkstra )

    POJ. 2253 Frogger (Dijkstra ) 题意分析 首先给出n个点的坐标,其中第一个点的坐标为青蛙1的坐标,第二个点的坐标为青蛙2的坐标.给出的n个点,两两双向互通,求出由1到2可行 ...

  3. 最短路(Floyd_Warshall) POJ 2253 Frogger

    题目传送门 /* 最短路:Floyd算法模板题 */ #include <cstdio> #include <iostream> #include <algorithm& ...

  4. POJ 2253 Frogger(dijkstra 最短路

    POJ 2253 Frogger Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fion ...

  5. dfs+记忆化搜索,求任意两点之间的最长路径

    C.Coolest Ski Route 题意:n个点,m条边组成的有向图,求任意两点之间的最长路径 dfs记忆化搜索 #include<iostream> #include<stri ...

  6. CCF(地铁修建):向前星+dijikstra+求a到b所有路径中最长边中的最小值

    地铁修建 201703-4 这题就是最短路的一种变形,不是求两点之间的最短路,而是求所有路径中的最长边的最小值. 这里还是使用d数组,但是定义不同了,这里的d[i]就是表示从起点到i的路径中最长边中的 ...

  7. Frogger POJ - 2253(求两个石头之间”所有通路中最长边中“的最小边)

    题意 ​ 题目主要说的是,有两只青蛙,在两个石头上,他们之间也有一些石头,一只青蛙要想到达另一只青蛙所在地方,必须跳在石头上.题目中给出了两只青蛙的初始位置,以及剩余石头的位置,问一只青蛙到达另一只青 ...

  8. poj 2253 Frogger (dijkstra最短路)

    题目链接:http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

  9. poj 2253 Frogger 解题报告

    题目链接:http://poj.org/problem?id=2253 题目意思:找出从Freddy's stone  到  Fiona's stone  最短路中的最长路. 很拗口是吧,举个例子.对 ...

随机推荐

  1. 实现textarea自适应的方法

    1.用div来模拟实现textarea自适应 <!doctype html> <html lang="en"> <head> <meta ...

  2. ADO.NET基本操作(CRUD、Procedure、Transaction)

    模型沿用上篇博客所提到的学生.教师.课程,以详细的代码进行演示. 增删改查 添加学生.教师.课程 using System.Data.SqlClient; namespace Test { class ...

  3. JavaScript ==和===

    == :  值等 === :恒等(引用等) ref: http://blog.csdn.net/wang171838/article/details/8554305 JavaScript支持“=”.“ ...

  4. Silverlight自动根据屏幕分辨率进行布局

    xaml: <UserControl x:Class="SLCenterLayout.MainPage" xmlns="http://schemas.microso ...

  5. Geoserver 相关学习

    参考资料: http://geoserver.org/ http://docs.geoserver.org/ 相关文档 http://docs.geoserver.org/stable/en/user ...

  6. scp实现mac与linux服务器之间文件传输

    1.mac上传文件到linux服务器 scp 文件名 用户名@服务器ip:目标路径如:scp /Users/test/testFile test@xxx.xxx.xxx.xxx:/test/ 2.ma ...

  7. 解决前端浏览器传JSON对像到服务端后全部变成string类型的方法

    这几天公司用nodejs+mongodb来做些东西,UI用的是kendo UI 碰到一个问题: 举个例子var a={"name":"张三","age ...

  8. 【笔记】W3C CSS关键属性

    white-space属性: white-space 属性设置如何处理元素内的空白. 可能的值 值 描述 normal 默认值,合并所有空格,换行符会被浏览器忽略 pre 空白会被浏览器保留.其行为方 ...

  9. Microsoft Access Database Engine 2010 Redistributable Download

    SQL Server 如需要导出类似Excel(*.xls.*.xlsx)格式的数据需要以来以下安装包 Microsoft Access 2010 数据库引擎可再发行程序包 此下载将安装一系列组件,帮 ...

  10. Python的循环

    循环是一个结构,导致一个程序要重复一定的次数 条件循环也一样,当条件变为假,循环结束 For循环 在python for循环遍历序列,如一个列表或一个字符. for循环语法:   ——for iter ...