Subway POJ

来源:互联网 发布:vscode 无法安装扩展 编辑:程序博客网 时间:2024/05/22 06:06
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
题目大意:一个人从家要到学校去,途中有许多车站,所以有步行和做地铁两种方式,其速度分别是10km/h 和40km/h。输入的规则是第一行输入的是x1,y1,x2,y2,分别代表家的坐标和学校的坐标。以后输入的是车站的坐标,数目不超过200,相邻的两个站点可以坐地铁,其他的需要步行。问到达学校的最短时间是多少?
         因为不知道输入的数据有多少,所以用while(scanf()!=EOF)。其他的就没有什么要注意的了,建图很重要。
#include<stdio.h>#include<string.h>#include<math.h>double mp[310][310],dis[310];int book[310];int n;int inf=0x3f3f3f3f;struct node{    double x,y;} st[10010];//结构体来存储站点信息void dj(){    memset(book,0,sizeof(book));    for(int i=0; i<n; i++)        dis[i]=mp[0][i];    book[0]=1;    for(int i=0; i<n-1; i++)    {        double Min=inf;        int x;        for(int j=0; j<n; j++)            if(!book[j]&&dis[j]<Min)            {                Min=dis[j];                x=j;            }        book[x]=1;        for(int k=0; k<n; k++)            if(mp[x][k]<inf&&dis[k]>dis[x]+mp[x][k])                dis[k]=dis[x]+mp[x][k];    }    printf("%.0f\n",dis[1]);}int main(){    while(~scanf("%lf%lf%lf%lf",&st[0].x,&st[0].y,&st[1].x,&st[1].y))//对于此题建图极为重要    {        for(int i=0; i<=300; i++)//先对整个图进行初始化            for(int j=0; j<=300; j++)                if(i==j) mp[i][j]=0;                else mp[i][j]=inf;        n=2;//因为是把整个数据输入到结构体中,而开始的两个点是起点与终点坐标        int k=0;//因为铁路线是一条直线,所以这里输入了两条,所以在计算的时候就要把他们分别计算        while(~scanf("%lf%lf",&st[n].x,&st[n].y))//处理的方法就是设置一个变量对于每条铁路线的第一个节点不进行计算        {            if(st[n].x==-1&&st[n].y==-1)//一条铁路线输入结束的话就把变量重新变为0;            {                k=0;                continue;            }            if(k==0)//如果k=0,就代表着他是这条铁路的第一个节点            {                k=1;                n++;                continue;            }            double r=sqrt((st[n].y-st[n-1].y)*(st[n].y-st[n-1].y)+(st[n].x-st[n-1].x)*(st[n].x-st[n-1].x));            mp[n][n-1]=mp[n-1][n]=r*3/2000;//直接把分钟计算出来            n++;        }        for(int i=0; i<n; i++)//这个题是以每个铁路站点作为节点进行计算的            for(int j=0; j<n; j++)//所以步行时间就是假如这两个地点之间没有路,就直接计算出来            {                if(mp[i][j]==inf)                {                    double r=sqrt((st[i].y-st[j].y)*(st[i].y-st[j].y)+(st[i].x-st[j].x)*(st[i].x-st[j].x));                    mp[i][j]=mp[j][i]=r*3/500;//对于步行时间的计算                }            }        dj();    }    return 0;}


原创粉丝点击