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. 

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 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

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;} 


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 京东店铺关门了怎么办 国美退款不到账怎么办 小米小店通过了怎么办 苹果的发票丢了怎么办 苹果7p开不开机怎么办 申请退款了怎么卖家还发货怎么办 买家申请退款卖家不退款怎么办 卖家恶意不退款怎么办 欠条到期了对方不还钱怎么办 冰箱磕了一坑怎么办 办信用卡没有家庭地址的怎么办 钱付了货没收到怎么办 在苏宁易购上买东西地址错了怎么办 手机分期付款银行卡丢了怎么办 华硕笔记本鼠标不动了怎么办 韵达快递不派送怎么办 中通快递不派送怎么办 农业银行信用卡密码输错三次怎么办 农业银行卡多次输错密码怎么办 想把店长弄走怎么办 济南银座卡过期了怎么办 银座购物卡丢失后怎么办 银座的卡丢了怎么办 银行卡换了旧卡怎么办 大理市民卡丢了怎么办 市民卡内的钱怎么办 宝付支付乱扣款怎么办 苏宁任性贷逾期怎么办 第二次跟家里开口要钱还网贷怎么办 网贷到家来要钱怎么办 网贷贷不了啦急要钱怎么办 百度推广竞价关键词太长怎么办 药店位置差客流少怎么办 网站上的用词违规怎么办 苹果16g内存不够怎么办 手机16g内存不够怎么办 在私企年纪大了怎么办 谷歌浏览器显示不安全打不开怎么办 4s密码多次错误怎么办 苹果4s手机系统错误怎么办 汽车充电口坏了怎么办