Question:
There is a n * m ski resort, and Bessie will slide from () to (n, m) and ask the minimum time.
There is a speed V at the beginning, and then every time from A to B at a point (only up, down, left, and right, one grid at a time ), the speed is multiplied by 2 ^ (weight a-weight B ).
The time consumed by each movement is the reciprocal of the current speed.
Question:
After analysis, we can find that the speed starting from a point is fixed after multiplication, multiplication, division, and division, that is, the speed from the starting point to the point is consistent, then we can build an undirected graph with fixed edge weight. Of course, the distance between a-> B and B-> A is basically impossible to be equal.
This question is very difficult. The first step is that the data range is large. # define INF 999999999999.99 is required.
Then, if it is wrong, pay attention to the intersection of C ++ and G ++, and then various outputs, such as. 2f and. 2lf.
Then, TLE may write a few more lines of code and add an PQ optimization.
In this case, I still handed in an online code, but I still need to paste my code, to help you understand, after all, the Code style is good.
#include <queue>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N 105#define NN 10100#define M 80000#define inf 999999999999.99#define eps 1e-6using namespace std;const int dx[4]={0,0,1,-1};const int dy[4]={1,-1,0,0};struct KSD{int v,next;double len;}e[M];int head[NN],cnt;void add(int u,int v,double len){cnt++;e[cnt].v=v;e[cnt].len=len;e[cnt].next=head[u];head[u]=cnt;}int id[N][N],num;int n,m;int map[N][N];double speed[N][N],p;int power(int x,int p){int ret=1;while(p){if(p&1)ret*=x;x*=x;p>>=1;}return ret;}double dist[NN];bool in[NN];struct Lux{double f;int v;Lux(double _f,int _v):f(_f),v(_v){}Lux(){}bool operator < (const Lux &a)const{return f<a.f;}};double spfa(int s,int t){int i,u,v;priority_queue<Lux>q;for(i=s;i<=t;i++)dist[i]=inf;dist[s]=0;in[s]=1;q.push(Lux(0,s));while(!q.empty()){Lux U=q.top();q.pop();u=U.v;in[u]=0;for(i=head[u];i;i=e[i].next){v=e[i].v;if(dist[v]>dist[u]+e[i].len+eps){dist[v]=dist[u]+e[i].len;if(!in[v]){in[v]=1;q.push(Lux(dist[v],v));}}}}return dist[t];}int main(){//freopen("test.in","r",stdin);int i,j,k;int a,b,c;int x,y,nid;scanf("%lf%d%d",&p,&n,&m);p=1.0/p;for(i=1;i<=n;i++)for(j=1;j<=m;j++){id[i][j]=++num;scanf("%d",&map[i][j]);if(map[i][j]>=map[1][1])speed[i][j]=p*power(2,map[i][j]-map[1][1]);else speed[i][j]=p/(double)power(2,map[1][1]-map[i][j]);}for(i=1;i<=n;i++)for(j=1;j<=m;j++){nid=id[i][j];for(k=0;k<4;k++){x=i+dx[k];y=j+dy[k];if(!id[x][y])continue;add(nid,id[x][y],speed[i][j]);}}printf("%.2f\n",spfa(1,n*m));return 0;}
Copy the Translation results to Google
[Poj3037] skiing Shortest Path