POJ

来源:互联网 发布:linux 启动文件丢失 编辑:程序博客网 时间:2024/06/03 13:45

You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of getting to ride your bike to school every day, you now get to walk and take the subway. Because you don't want to be late for class, you want to know how long it will take you to get to school. 
You walk at a speed of 10 km/h. The subway travels at 40 km/h. Assume that you are lucky, and whenever you arrive at a subway station, a train is there that you can board immediately. You may get on and off the subway any number of times, and you may switch between different subway lines if you wish. All subway lines go in both directions.
Input
Input consists of the x,y coordinates of your home and your school, followed by specifications of several subway lines. Each subway line consists of the non-negative integer x,y coordinates of each stop on the line, in order. You may assume the subway runs in a straight line between adjacent stops, and the coordinates represent an integral number of metres. Each line has at least two stops. The end of each subway line is followed by the dummy coordinate pair -1,-1. In total there are at most 200 subway stops in the city.
Output
Output is the number of minutes it will take you to get to school, rounded to the nearest minute, taking the fastest route.
Sample Input
0 0 10000 10000 200 5000 200 7000 200 -1 -1 2000 600 5000 600 10000 600 -1 -1
Sample Output
21

  这道题。。。我竟无言以对。先是翻译弄了半天,然后又研究了近一天。。 哭        哭  

首先,对于double型输出,C++  %lf  。。.G++  %f   目前发现的一点不同    

题意:你从家往学校赶,可以用步行和乘坐地铁这两种方式,步行速度为10km/h,乘坐地铁的速度为40KM/h。输入数据的第一行数据会给你起点和终点的x和y的坐标。然后会给你数目不超过200的双向地铁线路的站点,你可以从一个站点乘坐地铁到下一个站点,你可以在同一线路上乘坐地铁经过不同的站点,在不同线路的站点之间,你只能用步行的方式。让你求利用你可以利用的方式,求解到校花费的最短时间。

思路:没有思路快哭了,你信吗?反正我信。

   这道题算法隐藏的也很深啊,不能直接用。要自己构图,,第一次经历这样的题。实在想不起来,只有借鉴大牛的思路.........................

  大牛云:Dijkstra算法可以解决。需要特别注意以下输入数据的方式,这道题目没有很明显的输入结束标志,要用scanf()!= EOF来进行判断。然后把图的信息存入 map数组里面,当i到j在一条铁路线路上的时候,用它的距离除以40000来求解时间并存入map[i][j]中,map其他的位置存储不行的时间,然后套用模板直接求解最短路径就可以了。

还要注意:所有的点直接以步行的速度建边。地铁线路两站相邻的以地铁速度建边。

什么是没有很明显的输入结束标志,就是这道题运行不出结果,,,相当于是个死循环,自己怎么错的都不知道。可恶吧,还让我纠结了好久,直到直接用别人的AC代码试一下发火。构图很重要!!!要!!!

#include<stdio.h>#include<math.h>#define INF 0x3f3f3f3fdouble dis[500],map[500][500];int vis[500];int m;struct note{    double x,y;} q[10000];void Dijkstra(){    for(int i=0; i<m; i++) //cong 0 kai shi cun de    {        dis[i]=map[0][i];        vis[i]=0;    }    vis[0]=1;    for(int i=0; i<m-1; i++)    {        double min=INF;        int u=0;        for(int j=0; j<m; j++)        {            if(vis[j]==0&&dis[j]<min)            {                min=dis[j];                u=j;            }        }        vis[u]=1;        for(int j=1; j<m; j++)        {            if(map[u][j]<INF&&!vis[j]&&dis[j]>dis[u]+map[u][j])                dis[j]=dis[u]+map[u][j];        }    }    printf("%.0f\n",dis[1]*60.0);  //题目要求分钟。。 }int main(){    scanf("%lf%lf%lf%lf",&q[0].x,&q[0].y,&q[1].x,&q[1].y);//只有一组测试数据    m=2;    double a,b;    int flag=0;    for(int i=0; i<300; i++)        for(int j=0; j<300; j++)            if(i==j)map[i][j]=0;            else map[i][j]=INF;    while(~scanf("%lf%lf",&a,&b))//不知道有多少地铁,处理到文件结束    {        if(a==-1&&b==-1)   //为了相邻两站间计算        {            flag=0;            continue;        }        q[m].x=a,q[m].y=b;   //放心吧,一直循环,没结果。。。。。。        if(flag==0)        {            flag=1;            m++;            continue;        }        double temp=(sqrt((q[m].x-q[m-1].x)*(q[m].x-q[m-1].x)+(q[m].y-q[m-1].y)*(q[m].y-q[m-1].y)))/40000.0;//距离/速度 (h)        map[m][m-1]=map[m-1][m]=temp;                            //双向,构图,两站间        m++;    }    for(int i=0; i<m; i++)        for(int j=0; j<m; j++)            if(map[i][j]==INF)              //计算步行时间            {                map[i][j]=map[j][i]=(sqrt((q[j].x-q[i].x)*(q[j].x-q[i].x)+(q[j].y-q[i].y)*(q[j].y-q[i].y)))/10000.0;            }    Dijkstra();return 0;}


原创粉丝点击