POJ 3037 Skiing 二维spfa 模板

Skiing

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 5537

 

Accepted: 1464

 

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

Source

USACO 2005 October Gold

算法分析:

题意:

你在一个n*m网格的左上角,现在问你从左上角走到右下角需要的最少时间.其中网格中的任意两点的时间花费可以计算出来.

分析:

       首先我们需要证明的是从左上角出发到n*m网格中其他任意一点的速度都是固定的.对于下面的矩阵:

以题目样例为例子:

1 5 3

6 3 5

2 4 3

我们想计算到数值为6的点时的速度?

1->6的话 v6=v1*2^(1-6)

而从1->5 5->3 3->6 的话 v6=v1*2^(1-5) * 2^(5-3) * 2^(3-6)=v1*2^(1-6)

相等,同理我们可以证明到任意点的速度为:

 v(i,j)= v1*2^(1号点与该点的海拔之差)

       既然任意点的速度都是固定的,那么从该点到它的4个方向的边的时间开销也是固定的.

       所以,我们直接建立二维的spfa最短路。

代码实现:

#include<cstdio>  
#include<cstring>  
#include<cstdlib>  
#include<cctype>  
#include<cmath>  
#include<iostream>  
#include<sstream>  
#include<iterator>  
#include<algorithm>  
#include<string>  
#include<vector>  
#include<set>  
#include<map>  
#include<stack>  
#include<deque>  
#include<queue>  
#include<list>  
using namespace std;  
const double eps = 1e-8;  
typedef long long LL;  
typedef unsigned long long ULL;  
 
const int INT_M_INF = 0x7f7f7f7f;  
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;  
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;  
const int dx[] = {0, 0, -1, 1, -1, -1, 1, 1};  
const int dy[] = {-1, 1, 0, 0, -1, 1, -1, 1};  
const int MOD = 1e9 + 7;  
const double pi = acos(-1.0);  
const int MAXN = 1e5 + 10;  
const int MAXT = 10000 + 10; 
const double INF = 10000000000;     //注意初始化最大值类型为double
const int M=110;
int n,m,vis[M][M],mapp[M][M];
double d[M][M],v[M][M],V;  //d两点距离最小值,v为该点速度
struct node
{
	int x,y;
};
void spfa()
{
	queue<node> q;
	node temp1,temp2;
	
	for(int i=1;i<=n;i++)
	   for(int j=1;j<=m;j++)
	   {
	   	   d[i][j]=INF;
		   vis[i][j]=0;   
	   }
	temp1.x=1;temp1.y=1;
	q.push(temp1);
	vis[1][1]=1;
	d[1][1]=0;
	
	while(!q.empty())
	{
		temp1=q.front();
		q.pop();
		vis[temp1.x][temp1.y]=0;
		
		for(int i=0;i<=3;i++)
		{
			temp2=temp1;
			temp2.x+=dx[i];
			temp2.y+=dy[i];
			
			if(temp2.x<1||temp2.y<1||temp2.x>n||temp2.y>m)
			   continue;
			
			double w=(1.0)/v[temp1.x][temp1.y];   //计算时间
			if(d[temp1.x][temp1.y]+w<d[temp2.x][temp2.y])
			{
				d[temp2.x][temp2.y]=d[temp1.x][temp1.y]+w;
				if(!vis[temp2.x][temp2.y])
				{
				    q.push(temp2);
					vis[temp2.x][temp2.y]=1;
				}
			}   
		}
		
	}  	
}
int main()
{
    while(scanf("%lf%d%d",&V,&n,&m)!=EOF)
  {
      for(int i=1;i<=n;i++)
      	for(int j=1;j<=m;j++)
      {
        scanf("%d",&mapp[i][j]);
        v[i][j]=V*pow(2.0,double(mapp[1][1]-double(mapp[i][j])));
      }
      v[1][1]=V;
    spfa();
    printf("%0.2f\n",d[n][m]);
  }
   return 0;
}

猜你喜欢

转载自blog.csdn.net/sdz20172133/article/details/81267893