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 ...
随机推荐
- u-boot-2014.04分析
本文档以smdk2410为例初步分析了u-boot-2014.04的配置.启动流程.代码重定向.内存分布. u-boot-2014.04这个版本的uboot从Linux内核中借鉴了很多东西,比如编译u ...
- Java学习之路 第四篇 oop和class (面向对象和类)
本人水平有限,创作本文是为了记录学习和帮助初学者学习,欢迎指正和补充 一.面向对象编程的设计概述 很多同学都在学校学了电脑的编程,现在的书籍大部分都是oop面向对象编程,一个很抽象的的名字,比较难以理 ...
- React中key的必要性与使用
React这个框架的核心思想是,将页面分割成一个个组件,一个组件还可能嵌套更小的组件,每个组件有自己的数据(属性/状态);当某个组件的数据发生变化时,更新该组件部分的视图.更新的过程是由数据驱动的,新 ...
- python 基础 4.1 函数的参数
#/usr/bin/python #coding=utf-8 #@Time :2017/10/24 9:09 #@Auther :liuzhenchuan #@File :函数的参数.py # ...
- 【BZOJ1316】树上的询问 点分治+set
[BZOJ1316]树上的询问 Description 一棵n个点的带权有根树,有p个询问,每次询问树中是否存在一条长度为Len的路径,如果是,输出Yes否输出No. Input 第一行两个整数n, ...
- python之异步IO
协程的用武之地 并发量较大的系统和容易在IO方面出现瓶颈(磁盘IO,网络IO),采用多线程.多进程可以解决这个问题,当然线程.进程的切换时很消耗资源的.最好的解决方案是使用单线程方式解决并发IO问题- ...
- CSS定位细节
记住此三句话,很好的解决了关于浮动方面的问题: 1.浮动的元素对于没有设置浮动的元素来说是不存在的,浮动的元素将会覆盖没有浮动的元素 如图:先让d1浮动在left ,d2位置不浮动,d1覆盖了d2之上 ...
- Optimistic concurrency control
Optimistic concurrency control https://en.wikipedia.org/wiki/Optimistic_concurrency_control Optimist ...
- LigerUI java SSH小例子
1.新建web project 2.ssh框架 加入到项目中去(这里不介绍,网上搜索) 3.struts2配置 http://www.cnblogs.com/istianyu/archive/2013 ...
- mysql系列之5.mysql备份恢复
备份数据: mysqldump #mysqldump -uroot -p123456 test > /test_bak.sql #egrep -v "#|\*|--|^$" ...