题目链接:http://poj.org/problem?id=3037
题意:在只走上下走右的情况下,求从map[1][1]到map[r][c]的最短路;
ac代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
#define maxn 111
#define eps 1e-8
#define inf 999999999999
using namespace std;
int at[maxn][maxn];
int v,r,c;
int dic[][2]={0,1,1,0,-1,0,0,-1};
double mp[maxn][maxn];
double cost[maxn][maxn];
double dis[maxn][maxn];
bool vis[maxn][maxn];
struct node{
int x,y;
double dis;
bool operator < (const node &a) const {
return dis>a.dis;
}
};
double get_cost(int h)
{
return v*1.0*pow(2*1.0,at[1][1]-h);
}
void init()
{
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++)
dis[i][j]=inf;
memset(vis,0,sizeof(vis));
}
bool ok(int x,int y)
{
if(x>0&&x<=r&&y>0&&y<=c)
return true;
else
return false;
}
void dijkstra()
{
priority_queue< node> q;
node now,next;
now.x=1;
now.y=1;
now.dis=0;
dis[1][1]=0;
q.push(now);
while(!q.empty())
{
now=q.top();
q.pop();
if(vis[now.x][now.y])
continue;
vis[now.x][now.y]=true;
if(now.x==r&&now.y==c)
break;
int x;int y;
for(int i=0;i<4;i++)
{
x=now.x+dic[i][0];
y=now.y+dic[i][1];
if(!ok(x,y)) continue;
if(!vis[x][y]&&dis[x][y]-(now.dis+1.0/mp[now.x][now.y])>eps)
{
dis[x][y]=now.dis+1.0/mp[now.x][now.y];
next.x=x;
next.y=y;
next.dis=dis[x][y];
q.push(next);
}
}
}
}
int main()
{
scanf("%d%d%d",&v,&r,&c);
init();
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++)
scanf("%d",&at[i][j]);
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++)
mp[i][j]=get_cost(at[i][j]);
dijkstra();
printf("%.2f\n",dis[r][c]);
return 0;
}
利用搜索的思想去做题,at用于记录高度,mp记录各个点的相对速度
唯一的坑点就是poj的输出,,double必须用 %.2f 才可以。。。