POJ 2253 Frogger (求某两点之间所有路径中最大边的最小值)
题意:有两只青蛙,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 (求某两点之间所有路径中最大边的最小值)的更多相关文章
- POJ 2253 Frogger ,poj3660Cow Contest(判断绝对顺序)(最短路,floyed)
POJ 2253 Frogger题目意思就是求所有路径中最大路径中的最小值. #include<iostream> #include<cstdio> #include<s ...
- POJ. 2253 Frogger (Dijkstra )
POJ. 2253 Frogger (Dijkstra ) 题意分析 首先给出n个点的坐标,其中第一个点的坐标为青蛙1的坐标,第二个点的坐标为青蛙2的坐标.给出的n个点,两两双向互通,求出由1到2可行 ...
- 最短路(Floyd_Warshall) POJ 2253 Frogger
题目传送门 /* 最短路:Floyd算法模板题 */ #include <cstdio> #include <iostream> #include <algorithm& ...
- POJ 2253 Frogger(dijkstra 最短路
POJ 2253 Frogger Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fion ...
- dfs+记忆化搜索,求任意两点之间的最长路径
C.Coolest Ski Route 题意:n个点,m条边组成的有向图,求任意两点之间的最长路径 dfs记忆化搜索 #include<iostream> #include<stri ...
- CCF(地铁修建):向前星+dijikstra+求a到b所有路径中最长边中的最小值
地铁修建 201703-4 这题就是最短路的一种变形,不是求两点之间的最短路,而是求所有路径中的最长边的最小值. 这里还是使用d数组,但是定义不同了,这里的d[i]就是表示从起点到i的路径中最长边中的 ...
- Frogger POJ - 2253(求两个石头之间”所有通路中最长边中“的最小边)
题意 题目主要说的是,有两只青蛙,在两个石头上,他们之间也有一些石头,一只青蛙要想到达另一只青蛙所在地方,必须跳在石头上.题目中给出了两只青蛙的初始位置,以及剩余石头的位置,问一只青蛙到达另一只青 ...
- poj 2253 Frogger (dijkstra最短路)
题目链接:http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- poj 2253 Frogger 解题报告
题目链接:http://poj.org/problem?id=2253 题目意思:找出从Freddy's stone 到 Fiona's stone 最短路中的最长路. 很拗口是吧,举个例子.对 ...
随机推荐
- Oracle11g使用exp导出空表
1.Oracle11g默认对空表不分配segment,故使用exp导出Oracle11g数据库时,空表不会导出. 2.设置deferred_segment_creation 参数为FALSE后,无论是 ...
- DB2&&oracle-培训内容
DB2 1 Data Sharing架构,高可用 2 DB2的对象主要有以下几类:database,storage group, Tablspace, indexspace, table,index ...
- jacob 给word加印的功能
花了两天时间,参考了一些资料,总算是处理好了这样一个技术点. 关键的心得如下: 使用jacob,重点不是jacob本身,而是office的一些API资料.比如需要知道光标的移动, 包括上下左右的mov ...
- 【转】asp.net mvc3 简单缓存实现sql依赖
asp.net mvc3 简单缓存实现sql依赖 议题 随 着网站的发展,大量用户访问流行内容和动态内容,这两个方面的因素会增加平均的载入时间,给Web服务器和数据库服务器造成大量的请求压力.而大 ...
- Google Ajax Library API使用方法(JQuery)
Google Ajax Library API使用方法 1.传统方式: <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7. ...
- C# 网卡IP(网上资料整理)
//设置对外访问所使用网卡的IP string sendingIp = "192.168.0.1"; //设置对外访问所使用的端口 ; Uri uri = new Uri(&quo ...
- LINQ to XML(1)
LINQ to XML可以两种方式和XML配合使用.第一种方式是作为简化的XML操作API,第二种方式是使用LINQ查询工具.下面我使用的是第二种方式. 主要内容:用LINQ查询语句对XML文件里的数 ...
- Android L Ripple的使用
声明:Demo并不是有本人所写,本人只是总结在这里 工程源码: RippleDemo.zip ---------------------------------------------------- ...
- opencv的初体验
http://guoming.me/opencv-config 这篇文章有讲解opencv的安装与配置 一些常用库 opencv_core249d.lib opencv_imgproc249d.li ...
- linux下bus,device,driver三者关系
linux下bus,device,driver三者关系 1.bus: 总线作为主机和外设的连接通道,有些总线是比较规范的,形成了很多协议.如 PCI,USB,1394,IIC等.任何设备都可以选择合适 ...