Frogger(链式前向星+spfa小改动)
2015-10-18 23:04
155 查看
Description
Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists’ sunscreen, he wants to avoid swimming and instead reach her by jumping.
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
The input will contain one or more test cases. The first line of each test case will contain the number of stones n (2<=n<=200). The next n lines each contain two integers xi,yi (0 <= xi,yi <= 1000) representing the coordinates of stone #i. Stone #1 is Freddy’s stone, stone #2 is Fiona’s stone, the other n-2 stones are unoccupied. There’s a blank line following each test case. Input is terminated by a value of zero (0) for n.
Output
For each test case, print a line saying “Scenario #x” and a line saying “Frog Distance = y” where x is replaced by the test case number (they are numbered from 1) and y is replaced by the appropriate real number, printed to three decimals. Put a blank line after each test case, even after the last one.
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
一个青蛙从一个石头跳到目标石头,定义一条路的难度是这中间跳跃的最大距离,要求的是这些路线中,最大距离最小的那个。
其实就是把最短路比较中
d[v] = min(d[v],d[u] + w)改成d[v] = min(d[v],max(d[u],w));
Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists’ sunscreen, he wants to avoid swimming and instead reach her by jumping.
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
The input will contain one or more test cases. The first line of each test case will contain the number of stones n (2<=n<=200). The next n lines each contain two integers xi,yi (0 <= xi,yi <= 1000) representing the coordinates of stone #i. Stone #1 is Freddy’s stone, stone #2 is Fiona’s stone, the other n-2 stones are unoccupied. There’s a blank line following each test case. Input is terminated by a value of zero (0) for n.
Output
For each test case, print a line saying “Scenario #x” and a line saying “Frog Distance = y” where x is replaced by the test case number (they are numbered from 1) and y is replaced by the appropriate real number, printed to three decimals. Put a blank line after each test case, even after the last one.
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
一个青蛙从一个石头跳到目标石头,定义一条路的难度是这中间跳跃的最大距离,要求的是这些路线中,最大距离最小的那个。
其实就是把最短路比较中
d[v] = min(d[v],d[u] + w)改成d[v] = min(d[v],max(d[u],w));
#include<cstdio> #include<cstring> #include<iostream> #include<queue> #include<vector> #include<algorithm> #include<string> #include<cmath> #include<set> #include<map> #include<vector> using namespace std; typedef long long ll; const int inf = 0x3f3f3f3f; const int maxn = 1005; int head[202],leap[202]; double d[202]; struct Node { int x,y; }node[202]; struct Edge { int u,v,next; double w; }edge[80005]; double dis(int a,int b) { return sqrt(1.0*(node[a].x - node[b].x)*(node[a].x - node[b].x) + (node[a].y - node[b].y)*(node[a].y - node[b].y)); } void add_edge(int u,int v,double w,int tot) { edge[tot].u = u;edge[tot].v = v; edge[tot].w = w; edge[tot].next = head[u]; head[u] = tot; } int main() { #ifdef LOCAL freopen("C:\\Users\\巍巍\\Desktop\\in.txt","r",stdin); //freopen("C:\\Users\\巍巍\\Desktop\\out.txt","w",stdout); #endif // LOCAL int n,kase = 1; while(scanf("%d",&n) != EOF) { if(n == 0)break; for(int i = 1;i <= n;i++) scanf("%d%d",&node[i].x,&node[i].y); for(int i = 1;i <= n;i++)head[i] = -1; int tot = 1; for(int i = 1;i < n;i++) { for(int j = i + 1;j <= n;j++) { double d = dis(i,j); add_edge(i,j,d,tot++); add_edge(j,i,d,tot++); } } for(int i = 1;i <= n;i++)d[i] = inf; for(int i = 1;i <= n;i++)leap[i] = 0; queue<int>p; p.push(1); leap[1] = 1;d[1] = 0; while(p.size()) { int u = p.front();p.pop(); leap[u] = 0; for(int i = head[u];i != -1;i = edge[i].next) { int e = edge[i].v; double temp = max(edge[i].w,d[u]); if(d[e] > temp) { d[e] = temp; if(!leap[e]) { leap[e] = 1; p.push(e); } } } } printf("Scenario #%d\n",kase++); printf("Frog Distance = %.3lf\n\n",d[2]); } return 0; }
相关文章推荐
- nginx实现tcp反向代理
- I/O和XML
- Linux下的mongodb服务监视脚本(启动服务)
- Android sdk下载速度慢的问题
- MySQL 添加列,修改列,删除列
- 第七章软件静态测试
- Bootstrap插件transition源码的学习
- git使用之二
- Java集合框架
- ALM标准需求报告&需求跟踪矩阵
- 好用的网络请求库Retrofit2(入门及讲解)
- 枚举进程
- 整数反转代码
- DotNetOpenAuth实践之WCF资源服务器配置
- 零基础学python-19.12 总结列表解析与字典解析
- Command /Applications/Xcode 2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cla
- Alpha测试和Beta测试的理解和二者的区别
- 常用类和反射机制
- WebSphere7 XA 数据源配置问题
- 不爽 主要溢出 递归变化