POJ - 2253 Frogger(最短路Dijkstra or flod)
题意:要从起点的石头跳到终点的石头,设The frog distance为从起点到终点的某一路径中两点间距离的最大值,问在从起点到终点的所有路径中The frog distance的最小值为多少。
分析:
解法一:Dijkstra,修改最短路模板,d[u]表示从起点到u的所有路径中两点间距离的最大值的最小值。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-15;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 200 + 10;
const int MAXT = 10000 + 10;
using namespace std;
struct Edge{
int from, to;
double dist;
Edge(int f, int t, double d):from(f), to(t), dist(d){}
};
struct HeapNode{
double d;
int u;
HeapNode(double dd, int uu):d(dd), u(uu){}
bool operator < (const HeapNode& rhs)const{
return d > rhs.d;
}
};
struct Dijkstra{
int n, m;
vector<Edge> edges;
vector<int> G[MAXN];
double d[MAXN];
bool done[MAXN];
void init(int n){
this -> n = n;
for(int i = 0; i <= n; ++i) G[i].clear();
edges.clear();
}
void AddEdge(int from, int to, double dist){
edges.push_back(Edge(from, to, dist));
m = edges.size();
G[from].push_back(m - 1);
}
void dijkstra(int s){
priority_queue<HeapNode> Q;
for(int i = 0; i <= n; ++i){
d[i] = 10000000.0;
}
memset(done, false, sizeof done);
d[s] = 0;
Q.push(HeapNode(0, s));
while(!Q.empty()){
HeapNode x = Q.top();
Q.pop();
int u = x.u;
if(done[u]) continue;
done[u] = true;
for(int i = 0; i < G[u].size(); ++i){
Edge &e = edges[G[u][i]];
double tmp = max(d[u], e.dist);
if(tmp < d[e.to]) {
d[e.to] = tmp;
Q.push(HeapNode(d[e.to], e.to));
}
}
}
}
}dij;
struct Node{
int x, y;
void read(){
scanf("%d%d", &x, &y);
}
}num[MAXN];
double getD(Node& a, Node &b){
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
int main(){
int n;
int kase = 0;
while(scanf("%d", &n) == 1){
if(!n) return 0;
for(int i = 0; i < n; ++i) num[i].read();
dij.init(n);
for(int i = 0; i < n; ++i){
for(int j = i + 1; j < n; ++j){
double d = getD(num[i], num[j]);
dij.AddEdge(i, j, d);
dij.AddEdge(j, i, d);
}
}
dij.dijkstra(0);
printf("Scenario #%d\nFrog Distance = %.3f\n\n", ++kase, dij.d[1]);
}
return 0;
}
解法二:flod,pic[i][j]表示从i到j的所有路径中两点间距离的最大值的最小值。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 200 + 10;
const int MAXT = 10000 + 10;
using namespace std;
double pic[MAXN][MAXN];
struct Node{
int x, y;
void read(){
scanf("%d%d", &x, &y);
}
}num[MAXN];
double getD(Node& a, Node &b){
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
int main(){
int n;
int kase = 0;
while(scanf("%d", &n) == 1){
if(!n) return 0;
for(int i = 0; i < n; ++i) num[i].read();
for(int i = 0; i < n; ++i){
for(int j = i + 1; j < n; ++j){
double d = getD(num[i], num[j]);
pic[i][j] = pic[j][i] = d;
}
}
for(int k = 0; k < n; ++k){
for(int i = 0; i < n; ++i){
for(int j = i + 1; j < n; ++j){
if(pic[i][k] < pic[i][j] && pic[k][j] < pic[i][j]){
pic[j][i] = pic[i][j] = max(pic[i][k], pic[k][j]);
}
}
}
}
printf("Scenario #%d\nFrog Distance = %.3f\n\n", ++kase, pic[0][1]);
}
return 0;
}
POJ - 2253 Frogger(最短路Dijkstra or flod)的更多相关文章
- POJ 2253 Frogger -- 最短路变形
这题的坑点在POJ输出double不能用%.lf而要用%.f...真是神坑. 题意:给出一个无向图,求节点1到2之间的最大边的边权的最小值. 算法:Dijkstra 题目每次选择权值最小的边进行延伸访 ...
- POJ 2253 Frogger 最短路 难度:0
http://poj.org/problem?id=2253 #include <iostream> #include <queue> #include <cmath&g ...
- POJ 2253 Frogger (最短路)
Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28333 Accepted: 9208 Descript ...
- poj 2253 Frogger(最短路 floyd)
题目:http://poj.org/problem?id=2253 题意:给出两只青蛙的坐标A.B,和其他的n-2个坐标,任一两个坐标点间都是双向连通的.显然从A到B存在至少一条的通路,每一条通路的元 ...
- POJ 2253 Frogger ( 最短路变形 || 最小生成树 )
题意 : 给出二维平面上 N 个点,前两个点为起点和终点,问你从起点到终点的所有路径中拥有最短两点间距是多少. 分析 : ① 考虑最小生成树中 Kruskal 算法,在建树的过程中贪心的从最小的边一个 ...
- POJ 2253 Frogger(dijkstra 最短路
POJ 2253 Frogger Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fion ...
- 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 ,poj3660Cow Contest(判断绝对顺序)(最短路,floyed)
POJ 2253 Frogger题目意思就是求所有路径中最大路径中的最小值. #include<iostream> #include<cstdio> #include<s ...
- poj 2253 Frogger (dijkstra最短路)
题目链接:http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
随机推荐
- [Android]ListView中分割线的设置
1.在布局文件中ListView元素中通过属性设置 android:divider="#fffff" 分割线颜色 android:dividerHeight="1px& ...
- PHP 获取header 的自定义参数值
$.ajax({ type: "GET", url: "default.aspx", beforeSend: function(request) { reque ...
- saveToken介绍二
Struts的Token(令牌)机制能够很好的解决表单重复提交的问题,基本原理是:服务器端在处理到达的请求之前,会将请求中包含的令牌值与保存在当前用户会话中的令牌值进行比较,看是否匹配.在处理完该 ...
- SciPy 特殊函数
章节 SciPy 介绍 SciPy 安装 SciPy 基础功能 SciPy 特殊函数 SciPy k均值聚类 SciPy 常量 SciPy fftpack(傅里叶变换) SciPy 积分 SciPy ...
- Deepctr框架代码阅读
DeepCtr是一个简易的CTR模型框架,集成了深度学习流行的所有模型,适合学推荐系统模型的人参考. 我在参加比赛中用到了这个框架,但是效果一般,为了搞清楚原因从算法和框架两方面入手.在读代码的过程中 ...
- Tensorflow 将训练模型保存为pd文件
前言 保存 模型有2种方法. 方法 1.使用TensorFlow模型保存函数 save = tf.train.Saver() ...... saver.save(sess,"checkpoi ...
- Helm 架构【转】
在实践之前,我们先来看看 Helm 的架构. Helm 有两个重要的概念:chart 和 release. chart 是创建一个应用的信息集合,包括各种 Kubernetes 对象的配置模板.参数定 ...
- 5G/NR 频带详解
原文链接:http://www.sharetechnote.com/html/5G/5G_FR_Bandwidth.html 在NR中,3GPP中规定了大约两个大的频率范围.一个是我们通常所说的(su ...
- define可变参数,float数据传输
define可变参数 一般在调试打印Debug信息的时候, 需要可变参数的宏. 从C99开始可以使编译器标准支持可变参数宏(variadic macros), 另外GCC也支持可变参数宏, 但是两种在 ...
- swagger获取
参考 https://www.jianshu.com/p/840320d431a1 https://www.cnblogs.com/luoluocaihong/p/7106276.html