Skiing
2015-08-18 21:43
351 查看
Description
Bessie and the rest of Farmer John's cows are taking a trip this winter to go skiing. One day Bessie finds herself at the top left corner of an R (1 <= R <= 100) by C (1 <= C <= 100) grid of elevations E (-25 <= E <= 25). In order
to join FJ and the other cows at a discow party, she must get down to the bottom right corner as quickly as she can by travelling only north, south, east, and west.
Bessie starts out travelling at a initial speed V (1 <= V <= 1,000,000). She has discovered a remarkable relationship between her speed and her elevation change. When Bessie moves from a location of height A to an adjacent location of eight B, her speed is
multiplied by the number 2^(A-B). The time it takes Bessie to travel from a location to an adjacent location is the reciprocal of her speed when she is at the first location.
Find the both smallest amount of time it will take Bessie to join her cow friends.
Input
* Line 1: Three space-separated integers: V, R, and C, which respectively represent Bessie's initial velocity and the number of rows and columns in the grid.
* Lines 2..R+1: C integers representing the elevation E of the corresponding location on the grid.
Output
A single number value, printed to two exactly decimal places: the minimum amount of time that Bessie can take to reach the bottom right corner of the grid.
Sample Input
Sample Output
题解:求格子左上角到右下角最小值,方向为上下左右。既然是求最小值,自然想到最短路,但是用djistra的话,外层c*r-1,内层更新该点到其他点,c*r-1,明显不行。但是用spfa的话,每次更新邻接点(最多4个),这就可以了。
SPFA:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
const double INF = 1000000000; //小了不行
struct Node
{
int from;
int to;
Node(){
}
Node(int a,int b)
{
from = a;
to = b;
}
};
int map[120][120];
double d[120][120];
bool visited[120][120];
int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
void spfa(int c,int r,double v)
{
memset(visited,false,sizeof(visited));
for(int i = 1;i <= c;i++)
{
for(int j = 1; j<= r;j++)
{
d[i][j] = INF;
}
}
queue<Node> q;
q.push(Node(1,1));
d[1][1] = 0;
visited[1][1] = true;
while(!q.empty())
{
Node p = q.front();
q.pop();
visited[p.from][p.to] = false;
double t = 1.0 / (v * pow(2.0,map[1][1] - map[p.from][p.to])); //这里要注意是起点的速度的倒数才是到邻接点的时间
for(int i = 0;i < 4;i++)
{
int x = p.from + dir[i][0];
int y = p.to + dir[i][1];
if(x > 0 && x <= c && y > 0 && y <= r)
{
if(d[x][y] > d[p.f
4000
rom][p.to] + t)
{
d[x][y] = d[p.from][p.to] + t;
if(!visited[x][y])
{
q.push(Node(x,y));
visited[x][y] = true;
}
}
}
}
}
}
int main()
{
double v;
int c,r;
while(scanf("%lf%d%d",&v,&c,&r) != EOF)
{
for(int i = 1;i <= c;i++)
{
for(int j = 1;j <= r;j++)
{
scanf("%d",&map[i][j]);
}
}
spfa(c,r,v);
printf("%.2f\n",d[c][r]);
}
return 0;
}
Bessie and the rest of Farmer John's cows are taking a trip this winter to go skiing. One day Bessie finds herself at the top left corner of an R (1 <= R <= 100) by C (1 <= C <= 100) grid of elevations E (-25 <= E <= 25). In order
to join FJ and the other cows at a discow party, she must get down to the bottom right corner as quickly as she can by travelling only north, south, east, and west.
Bessie starts out travelling at a initial speed V (1 <= V <= 1,000,000). She has discovered a remarkable relationship between her speed and her elevation change. When Bessie moves from a location of height A to an adjacent location of eight B, her speed is
multiplied by the number 2^(A-B). The time it takes Bessie to travel from a location to an adjacent location is the reciprocal of her speed when she is at the first location.
Find the both smallest amount of time it will take Bessie to join her cow friends.
Input
* Line 1: Three space-separated integers: V, R, and C, which respectively represent Bessie's initial velocity and the number of rows and columns in the grid.
* Lines 2..R+1: C integers representing the elevation E of the corresponding location on the grid.
Output
A single number value, printed to two exactly decimal places: the minimum amount of time that Bessie can take to reach the bottom right corner of the grid.
Sample Input
1 3 3 1 5 3 6 3 5 2 4 3
Sample Output
29.00
题解:求格子左上角到右下角最小值,方向为上下左右。既然是求最小值,自然想到最短路,但是用djistra的话,外层c*r-1,内层更新该点到其他点,c*r-1,明显不行。但是用spfa的话,每次更新邻接点(最多4个),这就可以了。
SPFA:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
const double INF = 1000000000; //小了不行
struct Node
{
int from;
int to;
Node(){
}
Node(int a,int b)
{
from = a;
to = b;
}
};
int map[120][120];
double d[120][120];
bool visited[120][120];
int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
void spfa(int c,int r,double v)
{
memset(visited,false,sizeof(visited));
for(int i = 1;i <= c;i++)
{
for(int j = 1; j<= r;j++)
{
d[i][j] = INF;
}
}
queue<Node> q;
q.push(Node(1,1));
d[1][1] = 0;
visited[1][1] = true;
while(!q.empty())
{
Node p = q.front();
q.pop();
visited[p.from][p.to] = false;
double t = 1.0 / (v * pow(2.0,map[1][1] - map[p.from][p.to])); //这里要注意是起点的速度的倒数才是到邻接点的时间
for(int i = 0;i < 4;i++)
{
int x = p.from + dir[i][0];
int y = p.to + dir[i][1];
if(x > 0 && x <= c && y > 0 && y <= r)
{
if(d[x][y] > d[p.f
4000
rom][p.to] + t)
{
d[x][y] = d[p.from][p.to] + t;
if(!visited[x][y])
{
q.push(Node(x,y));
visited[x][y] = true;
}
}
}
}
}
}
int main()
{
double v;
int c,r;
while(scanf("%lf%d%d",&v,&c,&r) != EOF)
{
for(int i = 1;i <= c;i++)
{
for(int j = 1;j <= r;j++)
{
scanf("%d",&map[i][j]);
}
}
spfa(c,r,v);
printf("%.2f\n",d[c][r]);
}
return 0;
}
相关文章推荐
- IP地址按数字大小排序
- linux学习技巧
- OS层面的恢复(数据库在Open的状态,删除表空间)
- 如何查看电脑的IP地址
- 初识 GitHub
- linux下查看文件夹的大小
- http实现虚拟主机的配置
- Centos7 把php5.4升级到php5.6
- sdnu 1078 食物链(并查集)
- 多线程dp hdu2686 Matrix
- java字符串
- 嵌入式中最重要的四个词语static,extern,volatile,const
- HDU 2084 数塔 直接求解
- 类、字段和方法
- 欢迎使用CSDN-markdown编辑器
- 多线程
- 预定🏨
- 找回密码思路
- 一个Sqrt谋杀触发功能
- list转json的一些问题