POJ2253(djkstra求最长最短边)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 32257 | Accepted: 10396 |
Description
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 题意:求结点1到结点2所有每条路径最长的边中的最短的边。
#include"cstdio"
#include"cmath"
using namespace std;
double Max(double x,double y)
{
if(x>y) return x;
else return y;
}
const int MAXN=;
const int INF=0x3fffffff;
struct Node{
int x,y,index;
}a[MAXN];
double mp[MAXN][MAXN];
double distance(int i,int j)
{
return sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y));
}
int main()
{
int cas=;
int n;
while(scanf("%d",&n)!=EOF&&n)
{
for(int i=;i<n;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
a[i].index=i+;
}
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
mp[a[i].index][a[j].index]=distance(i,j);
}
}
for(int k=;k<=n;k++)
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(mp[k][j]<mp[i][j]&&mp[i][k]<mp[i][j])
{
mp[i][j]=Max(mp[k][j],mp[k][i]);//mp[i][j]存放i->j路径中的最长边
} printf("Scenario #%d\n",cas++);
printf("Frog Distance = %0.3f\n",mp[][]);
printf("\n");
}
return ;
}
dijkstra:
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAXN=;
const int INF=0x3f3f3f3f;
struct Node{
int x,y;
}stone[MAXN];
int n;
double mp[MAXN][MAXN];
double dist(int x1,int y1,int x2,int y2)
{
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
double d[MAXN];
int vis[MAXN];
double dijkstra(int s)
{
for(int i=;i<=n;i++)
{
d[i]=mp[s][i];
vis[i]=;
}
int t=n;
while(t--)
{
double mincost=INF;
int k;
for(int i=;i<=n;i++)
{
if(!vis[i]&&mincost>d[i])
{
mincost=d[i];
k=i;
}
}
vis[k]=;
for(int i=;i<=n;i++)
{
if(!vis[i]&&d[i]>max(d[k],mp[k][i]))
{
d[i]=max(d[k],mp[k][i]);
}
}
}
return d[];
}
int main()
{
int t=;
while(scanf("%d",&n)!=EOF&&n!=)
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(i==j) mp[i][j]=;
else mp[i][j]=INF; for(int i=;i<=n;i++)
{
scanf("%d%d",&stone[i].x,&stone[i].y);
}
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
double d=dist(stone[i].x,stone[i].y,stone[j].x,stone[j].y);
mp[i][j]=mp[j][i]=d;
}
} printf("Scenario #%d\n",++t);
printf("Frog Distance = %.3f\n\n",dijkstra());//.lf会WA
}
return ;
}
POJ2253(djkstra求最长最短边)的更多相关文章
- AC日记——最长最短单词 openjudge 1.7 25
25:最长最短单词 总时间限制: 1000ms 内存限制: 65536kB 描述 输入1行句子(不多于200个单词,每个单词长度不超过100),只包含字母.空格和逗号.单词由至少一个连续的字母构成 ...
- spfa求最长路
http://poj.org/problem?id=1932 spfa求最长路,判断dist[n] > 0,需要注意的是有正环存在,如果有环存在,那么就要判断这个环上的某一点是否能够到达n点,如 ...
- Manacher算法 - 求最长回文串的利器
求最长回文串的利器 - Manacher算法 Manacher主要是用来求某个字符串的最长回文子串. 不要被manacher这个名字吓倒了,其实manacher算法很简单,也很容易理解,程序短,时间复 ...
- 后缀数组 - 求最长回文子串 + 模板题 --- ural 1297
1297. Palindrome Time Limit: 1.0 secondMemory Limit: 16 MB The “U.S. Robots” HQ has just received a ...
- 后缀数组(模板题) - 求最长公共子串 - poj 2774 Long Long Message
Language: Default Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 21 ...
- HDU 1025 Constructing Roads In JGShining's Kingdom(求最长上升子序列nlogn算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 解题报告:先把输入按照r从小到大的顺序排个序,然后就转化成了求p的最长上升子序列问题了,当然按p ...
- HDU 4612 Warm up tarjan缩环+求最长链
Warm up Problem Description N planets are connected by M bidirectional channels that allow instant ...
- [algorithm]求最长公共子序列问题
最直白方法:时间复杂度是O(n3), 空间复杂度是常数 reference:http://blog.csdn.net/monkeyandy/article/details/7957263 /** ** ...
- hdu 3068 最长回文(manachar求最长回文子串)
题目连接:hdu 3068 最长回文 解题思路:通过manachar算法求最长回文子串,如果用遍历的话绝对超时. #include <stdio.h> #include <strin ...
随机推荐
- jquery在网页实时显示时间;
1.定义一个显示时间的位置 <div id="shijian"> </div> 2.jquery代码 function showTime() { var c ...
- 广播、多播和IGMP的一点记录
广播和多播:仅应用于UDP 广播分为: 1.受限的广播(255.255.255.255) 2.指向网络的广播(eg:A类网络 netid.255.255.255)主机号为全1的地址 3.指向子网的广播 ...
- EasyPlayer RTSP Android安卓播放器实现视频源快速切换
EasyPlayer现在支持多视频源快速切换了,我们介绍一下是如何实现的. 这个需求通常应用在一个客户端需要查看多个视频源的情况,比如多个监控场景轮播. 由于EasyPlayer的播放端已经放在Fra ...
- EasyNVR无插件IPC摄像机直播方案前端构建之:如何区分PC端和移动端
EasyNVR前端为了更好的用户体验,不仅仅设有PC客户端,还适应移动客户端: EasyNVR的客户端中PC端和移动端差异有很多.例如: 由于PC端.移动端自身硬件的差异,所需要展示的样式也会存在一定 ...
- Greedy Function Approximation:A Gradient Boosting Machine
https://statweb.stanford.edu/~jhf/ftp/trebst.pdf page10 90% to 95% of the observations were often de ...
- 2017-2018-1 20179209《Linux内核原理与分析》第七周作业
一.实验 1.1task_struct数据结构 Linux内核通过一个被称为进程描述符的task_struct结构体来管理进程,这个结构体包含了一个进程所需的所有信息.它定义在linux-3.18.6 ...
- 2017-2018-1 20179209《Linux内核原理与分析》第三周作业
一.函数调用堆栈 存储程序.函数调用堆栈(高级语言起点)和中断机制是计算机工作的三大法宝.其中函数调用堆栈是本次学习的重点.先介绍一些基本的知识点: 1.ebp 在C语言中用作记录当前函数调用的基址: ...
- ABAP div / mod的用法
1.divdiv是用于取两数相除的商的,c = a div b,得到的c的值就是a除b的商.2.// 是用于取两数相除的结果的.c = a / b,如果c是i数据类型的,这个语法会进行四舍五入的.3. ...
- Java反射详解(转)
原文地址:http://www.importnew.com/17616.html 动态语言 动态语言,是指程序在运行时可以改变其结构:新的函数可以被引进,已有的函数可以被删除等在结构上的变化.比如众所 ...
- SQL join中级篇--hive中 mapreduce join方法分析
1. 概述. 本文主要介绍了mapreduce框架上如何实现两表JOIN. 2. 常见的join方法介绍 假设要进行join的数据分别来自File1和File2. 2.1 reduce side jo ...