poj-3070 skiing
来源:互联网 发布:js分页思路 编辑:程序博客网 时间:2024/06/07 14:00
Skiing
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 5228 Accepted: 1389 Special Judge
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.
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.
* 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 31 5 36 3 52 4 3
Sample Output
29.00
Hint
Bessie's best route is:
Start at 1,1 time 0 speed 1
East to 1,2 time 1 speed 1/16
South to 2,2 time 17 speed 1/4
South to 3,2 time 21 speed 1/8
East to 3,3 time 29 speed 1/4
Start at 1,1 time 0 speed 1
East to 1,2 time 1 speed 1/16
South to 2,2 time 17 speed 1/4
South to 3,2 time 21 speed 1/8
East to 3,3 time 29 speed 1/4
Source
USACO 2005 October Gold
思路:这个题用到物理上势能与动能转化的知识点, 因为不是一维的,所以不会用Dijkstra,这个题有两种思路
1. 利用BFS+优先队列, 即每次从时间最少的点搜索,直到搜到目标点,值得注意的是,将这个点的vis设置为“1”的条件是这个点是最少时间点,而不是走到哪个哪个设为“1”;
2. 用spfa; 话不多说上代码
#include<stdio.h>#include<algorithm>#include<cstring>#include<math.h>#include<queue>using namespace std;const int maxn=110;const int INF=0x3f3f3f3f;int map[maxn][maxn], vis[maxn][maxn];int n, m, dir[4][2]={{0, 1}, {0, -1}, {1, 0}, {-1, 0}};double v;struct node{int x, y;double t;bool operator < (const node& tmp) const{return t>tmp.t;}};void BFS(){priority_queue<node> q;q.push((node){1, 1, 0.0});while(q.size()){node it=q.top();q.pop();if(vis[it.x][it.y]) continue;vis[it.x][it.y]=1;if(it.x==n && it.y==m){printf("%.2f\n", it.t);return ;}for(int i=0; i<4; i++){int tx=it.x+dir[i][0];int ty=it.y+dir[i][1];if(tx<1 || tx>n || ty<1 || ty>m)continue;if(!vis[tx][ty]){double t=it.t + 1.0/(pow(2.0, (map[1][1]-map[it.x][it.y])*1.0)*v);q.push((node){tx, ty, t});}}}}int main(){scanf("%lf%d%d", &v, &n, &m);for(int i=1; i<=n; i++)for(int j=1; j<=m; j++)scanf("%d", &map[i][j]);BFS();return 0;}
#include<stdio.h>#include<math.h>#include<queue>using namespace std;const double INF=0x3f3f3f3f3f;//注意因为是double类型, 所以要设的大一点const int maxn = 110;int mp[maxn][maxn] , vis[maxn][maxn];double time[maxn][maxn];int dir[4][2]={{1, 0}, {-1, 0}, {0, -1}, {0, 1}};struct node{int x, y;};void spfa(int n, int m, double v){time[1][1]=0;queue<node> q;q.push((node){1, 1});vis[1][1]=1;while(!q.empty()){node it=q.front();q.pop();vis[it.x][it.y]=0;double t=1.0/(v*pow(2.0, mp[1][1]-mp[it.x][it.y]));for(int i=0; i<4; i++){int tx=it.x+dir[i][0];int ty=it.y+dir[i][1];if(tx<1 || tx>n || ty<1 || ty>m)continue;if(time[tx][ty]>time[it.x][it.y]+t){time[tx][ty]=time[it.x][it.y]+t;if(!vis[tx][ty]){q.push((node){tx, ty});vis[tx][ty]=1;}}}}}int main(){double v;int r, c;scanf("%lf%d%d", &v, &r, &c);for(int i=1; i<=r; i++){for(int j=1; j<=c; j++){scanf("%d", &mp[i][j]);time[i][j]=INF;vis[i][j]=0;}}spfa(r, c, v);printf("%.2f\n", time[r][c]);return 0;}
阅读全文
0 0
- poj-3070 skiing
- Skiing POJ
- poj 3037 Skiing
- Poj 3037 Skiing
- POJ 3037 Skiing
- poj 3037 Skiing
- POJ-3037-Skiing
- POJ 3037 Skiing(Dijkstra)
- POJ 3037 Skiing SPFA
- POJ -- 3037 Skiing
- POJ 3037 Skiing(Dijkstra)
- POJ 3037 Skiing
- poj 3037 -- Skiing spfa
- Skiing
- skiing
- skiing
- skiing
- Skiing
- Html&css刷题知识记录
- Bootstrap常用类名总结
- Spring Cloud Eureka 服务治理(一)
- 设计模式之观察者模式
- hdu 6199 gems gems gems
- poj-3070 skiing
- Capacity Scheduler配置说明
- 微信小程序全局变量
- Spring 框架
- 归并排序 --C语言实现
- mysql常用函数汇总(转载)
- 【Java概念】抽象类(10)
- sails-mongo 使用 groupBy 进行分组遇到的坑
- Tomcat 6中配置BoneCP数据源