ZZUOJ 10496: Cross the River (最短路 Dij 青蛙过河)

来源:互联网 发布:java 微信网页授权 编辑:程序博客网 时间:2024/06/08 06:03

郑州大学第十届ACM大学生程序设计竞赛正式赛  ——E题

10496: Cross the River

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 53 Solved: 6
[Submit][Status][Web Board]

Description

A frog wants to cross the river.The frog’s plane is determined by a rectangular coordinate system.The two sides of the river are respectively corresponding to the line y=0 and y=d (d>0).The frog is located in any coordinate which satisfies y<=0.The frog’s hop distance has a limit W.Now it wants to jump to the other side through the lotus leaves.Please select a reasonable route to minimize the journey.

Input

There are multiple test cases and the first line is the number of test cases T(1<=T<=10). For each test case,first line contains three integers d,w and n(0<d,w<=30000,0<n<=500,n is the number of lotus leaves), and in next n lines, the ith line contains the coordinate of the ith lotus leaf (xi,yi).(xi and yi are integers, and 0<yi<d )

Output

For each test case, output one line, the minimum of the distance.(The result keeps five decimal places)

If the frog can’t touch the other side,print “-1”.

Sample Input

1
6 3  
1 2
2 4

Sample Output

6.23607

HINT

Source

吐槽:周末闲的,临时被WY拉去打郑大的比赛,拿到这道时,一眼看到  A frog wants to cross the river,还用猜肯定是最短路啦,之前也写过不少这种题吧,当时看榜没几个人写,翻译时又有好几个单词不认识,尴尬啊.....所以选择先放放。等最后看题时才知道如此之水,坑的就是好久没刷题了,最短路的模板都没有,尴尬的Dijkstra都忘了。这酱油打的....

题意:青蛙过河,河宽为d,青蛙最远能跳w,河里有n块石头可踩,给出n个石头的坐标。
分析:其实就是个简单的模板题,我们只要考虑一下,青蛙起始位置是在y=0下面,最后跳到y=d上面即可,那么我们只需要在原始的maps地图中加上起始点为每个点到x轴的距离,和到终止点的距离即每个点到y=d的距离(当然也要满足在青蛙的弹跳范围w以内)。

代码:
#include<iostream>#include<string.h>#include<algorithm>#include<math.h>#include<stdio.h>#define INF 0x3f3f3f3fusing namespace std;#define N 1100int n,d,w;double maps[N][N],dist[N];int vis[N];struct node{    int x,y;} p[N];double Dist(int a,int b){    return sqrt((p[a].x-p[b].x)*(p[a].x-p[b].x)+(p[a].y-p[b].y)*(p[a].y-p[b].y));}void init(){    for(int i=0; i<=n+1; i++)    {        for(int j=0; j<=n+1; j++)        {            maps[i][j]=maps[j][i]=(i==j? 0 : INF);        }        vis[i]=0;    }}void Dij(){    int index;    for(int i=0;i<=n+1;i++)        dist[i]=maps[0][i];    vis[0]=1;    for(int i=0;i<=n+1;i++)    {        double MIN=INF;        for(int j=0;j<=n+1;j++)        {            if(!vis[j]&&dist[j]<MIN)            {                MIN=dist[j];                index=j;            }        }        vis[index]=1;        for(int j=0;j<=n+1;j++)        {            if(!vis[j]&&dist[index]+maps[index][j]<dist[j])                dist[j]=dist[index]+maps[index][j];        }    }    if(dist[n+1]==INF)        printf("-1\n");    else        printf("%.5f\n",dist[n+1]);}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d%d",&d,&w,&n);        init();        memset(p,0,sizeof(p));        for(int i=1; i<=n; i++)        {            scanf("%d%d",&p[i].x,&p[i].y);        }        if( d < w )             maps[0][n+1]=maps[n+1][0]=d;        for(int i=0; i<=n+1; i++)        {            if(i==0)            {                for(int j=1; j<=n; j++)                {                    if(w >= p[j].y)                        maps[i][j]=maps[j][i]=p[j].y;                }            }            else if(i==n+1)            {                for(int j=1; j<=n; j++)                {                    if(w >= (d-p[j].y))                        maps[i][j]=maps[j][i]=d-p[j].y;                }            }            else            {                for(int j=i+1; j<=n; j++)                {                    if(Dist(i,j) <= w )                        maps[i][j]=maps[j][i]=min(maps[i][j],Dist(i,j));                }            }        }        Dij();    }    return 0;}




0 0
原创粉丝点击