日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【POJ - 3037】Skiing (Dijkstra算法)

發布時間:2023/12/10 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【POJ - 3037】Skiing (Dijkstra算法) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

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

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

?

解題報告:

? ? ? 很坑的一道單源最短路。Dijkstra或Spfa均可解。思想和這題類似【HDU - 1546】 Idiomatic Phrases Game(Dijkstra)

AC代碼:

#include <iostream> #include<cstring> #include<cmath> #include<cstdio> #include<queue> #include <cfloat> using namespace std;int r,c; double v; const double INF = 0x3f3f3f3f; const double eps = 1e-8; double dis[105][105]; int maze[105][105]; bool vis[105][105];//此題以二維數組表示一個點,就不對每個點打標記了。。。比如3*3的矩陣,打標記就是點1~9,此題不這樣做了,,不然太麻煩。 int head[105]; int nx[4] = {0,1,0,-1}; int ny[4] = {1,0,-1,0}; //即,此題把一維表示點,都擴展成二維即可。 此題用二維表示點 struct Point {int x,y;double t;bool operator<(const Point & b) const {return t>b.t; }Point(){}Point(int x,int y,double t):x(x),y(y),t(t) {} }p; //默認從(1,1)跑到(r,c); void Dijkstra() {dis[1][1] = 0; // int all = r*c;//共r*c個點 double minw = INF;priority_queue<Point > pq;pq.push(Point(1,1,0));while(!pq.empty()) {int nxx,nyy;//找點 Point cur = pq.top();pq.pop();if(vis[cur.x][cur.y] == 1) continue;vis[cur.x][cur.y] = 1;if(cur.x == r && cur.y == c) break;for(int k = 0; k<4; k++) {nxx = cur.x+nx[k];nyy = cur.y+ny[k];if(vis[nxx][nyy] == 1) continue;if(nxx<1 || nxx>r || nyy<1 || nyy>c) continue;double t = cur.t + 1.0 / ( pow(2,maze[1][1]-maze[cur.x][cur.y])*v );if(dis[nxx][nyy] > t) {dis[nxx][nyy] = t;pq.push(Point(nxx,nyy,dis[nxx][nyy]));} // dis[nxx][nyy] = dis[nxx][nyy] == INF ? t : min(t,dis[nxx][nyy]); // pq.push(Point(nxx,nyy,dis[nxx][nyy])); }}printf("%.2f\n",dis[r][c]); } void init() {memset(vis,0,sizeof(vis)); // memset(dis,INF,sizeof(dis));for(int i = 1; i<=r; i++) {for(int j = 1; j<=c; j++) {dis[i][j] = DBL_MAX;}}memset(maze,0,sizeof(maze)); } int main() {scanf("%lf%d%d",&v,&r,&c); init();for(int i = 1; i<=r; i++) {for(int j = 1; j<=c; j++) {scanf("%d",&maze[i][j]);}}Dijkstra() ;return 0 ; }

總結:?

總結

以上是生活随笔為你收集整理的【POJ - 3037】Skiing (Dijkstra算法)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。