poj2502 Subway

来源:互联网 发布:webclient post json 编辑:程序博客网 时间:2024/05/16 07:15

文章大意:第一行是你家和你的学校的坐标,然后每一行是地铁的站点的坐标,相邻的站点可以乘地铁到达速度40km/h,其他站点只能步行到达,速度10km/h,问你从家到学校最短的时间是多少


大致思路:难点是输入数据,你用测试数据的时候是输不出结果的,但是能提交对,前提你的代码是对的。用dijkstra,将站点的距离存入数组,其他的就完全是dijkstra的模板了

You have just moved from a quietWaterloo 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
#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>using namespace std;double e[230][230],dis[230];int book[230],n;double inf=10000000.0;struct node{    double x,y;} q[10010];void dijkstra(){    memset(book,0,sizeof(book));    int i,j,k,u,v;    for(i=0; i<n; i++)        dis[i]=e[0][i];    book[0]=1;    for(i=0; i<n-1; i++)    {        double minn=inf;        for(j=0; j<n; j++)        {            if(book[j]==0&&dis[j]<minn)            {                minn=dis[j];                u=j;            }        }        book[u]=1;        for(v=0; v<n; v++)        {            if(e[u][v]<inf)            {                if(dis[v]>dis[u]+e[u][v])                    dis[v]=dis[u]+e[u][v];            }        }    }    printf("%.0f\n",dis[1]);}int main(){    int i,j,m;    while(~scanf("%lf%lf%lf%lf",&q[0].x,&q[0].y,&q[1].x,&q[1].y))    {        for(i=0; i<=230; i++)            for(j=0; j<=230; j++)                if(i==j)e[i][j]=0;                else e[i][j]=inf;        n=2;m=0;        while(~scanf("%lf%lf",&q[n].x,&q[n].y))        {            if(q[n].x==-1&&q[n].y==-1)            {                m=0;                continue;            }            if(!m)            {                m=1;                n++;                continue;            }            double w=sqrt((q[n].x-q[n-1].x)*(q[n].x-q[n-1].x)+(q[n].y-q[n-1].y)*(q[n].y-q[n-1].y));            // printf("%lf/*/*/*/*/\n",w);            e[n-1][n]=e[n][n-1]=w*6/4000;            n++;        }        for(i=0; i<n; i++)            for(j=0; j<n; j++)                if(e[i][j]==inf)                {                    double r=sqrt((q[i].x-q[j].x)*(q[i].x-q[j].x)+(q[i].y-q[j].y)*(q[i].y-q[j].y));                    e[j][i]=e[i][j]=r*6/1000;                }        dijkstra();    }    return 0;}


原创粉丝点击