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 ...
随机推荐
- Qt QImageReader 相似乎有bug
Qt 版本号 5.4.1 (VS2010) 近期在做一个小程序,事实上非常easy,就是打开一个gif 动画.能够静态显示当中随意一帧图像.Qt 中有一个QImageReader 类.用这个类理论上说 ...
- 【Unity 3D】学习笔记三十:游戏元素——游戏地形
游戏地形 在游戏的世界中,必然会有非常多丰富多彩的游戏元素融合当中. 它们种类繁多.作用也不大同样.一般对于游戏元素可分为两种:经经常使用.不经经常使用.经常使用的元素是游戏中比較重要的元素.一般须要 ...
- HDOJ 题目3564 Another LIS(线段树单点更新,LIS)
Another LIS Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- 矩阵十题【六】 poj3070 Fibonacci
题目链接:http://poj.org/problem? id=3070 题目大意:给定n和10000,求第n个Fibonacci数mod 10000 的值,n不超过2^31. 结果保留四位数字. 非 ...
- ios中实现对UItextField,UITextView等输入框的字数限制
本文转载至 http://blog.sina.com.cn/s/blog_9bf272cf01013lsd.html 2011-10-05 16:48 533人阅读 评论(0) 收藏 举报 1. ...
- 九度OJ 1157:中位数 (中位数、排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2188 解决:1294 题目描述: 中位数定义:一组数据按从小到大的顺序依次排列,处在中间位置的一个数(或最中间两个数据的平均数). 给出一 ...
- iOS与H5交互及UIWebView缓存
iOS原生App与H5页面交互笔记 最近在做一个项目用到了原生App与H5交互,之前有做过简单的H5页面直接调用原生方法的例子,就是利用UIWebView中的代理方法 //webview每次加载之前都 ...
- php.ini的几个关键配置
safe_mode = On safe_mode_gid = Off disable_functions = system,passthru,exec,shell_exec,popen,phpinfo ...
- 洛谷2483 k短路([SDOI2010]魔法猪学院)
题目请戳这里 一句话题意: 给你一张n个节点,m条单向边的图,求1到n第k短的路. emmm,纪念第一个黑题(我是真的菜啊!!) 这题目还是很难的,本蒟蒻只会被洛谷卡掉的A(所以就愉快地特判了),首先 ...
- Nginx + Tomcat 应用证书启用 SSL
第一部分 简述 - 附:相关概念 1 Nginx 是什么? - 2 Tomcat 是什么? - 3 SSL 是什么? Secure Sockets Layer,现在应该叫"TLS" ...