Floyd算法  POJ2502

来源:互联网 发布:ug编程教程百度云 编辑:程序博客网 时间:2024/06/05 15:44
SubwayTime Limit: 1000MSMemory Limit: 65536KTotal Submissions: 11572Accepted: 3775DescriptionYou 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.InputInput 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. OutputOutput is the number of minutes it will take you to get to school, rounded to the nearest minute, taking the fastest route.Sample Input0 0 10000 10000 200 5000 200 7000 200 -1 -1 2000 600 5000 600 10000 600 -1 -1Sample Output21SourceWaterloo local 2001.09.22我真心被这一题坑了,整整debug了近5个小时,WA的原因有两个,1是没考虑溢出,Int型应该会炸,所以用了double。还有就是理解错了题目意思,只有相邻两站才可以坐地铁,也就是意味着假如地铁有1,2,3 三站,那么t(1,3)=(S(1,3)/V人)。我本来打算先用floyd试试,炸的话就换成dijk;后来发现这一题精华其实是建模。图建好以后,其他的就很简单了。附上floyd算法代码:(还可以清楚的看到我debug的痕迹,几乎把很多中间变量都打出来了QAQ)#include<cstdio>#include<cmath>#define MAX 205#define MAXN 0xfffffffdouble V1=10000/60;double V2=40000/60;struct Station{    double x;    double y;}; Station stop[MAX];double map[MAX][MAX];int main(){//  freopen("test.txt","r",stdin);    for(int i=0;i<MAX;i++)        for(int j=0;j<MAX;j++)            {                if(i==j)                    map[i][j]=0;                else                    map[i][j]=MAXN;             }    double temp1,temp2;    scanf("%lf%lf",&stop[1].x ,&stop[1].y );    scanf("%lf%lf",&stop[2].x ,&stop[2].y );    int i=3;    int cnt=3;    int line[MAX]={0};    line[1]=line[2]=1;    int stationnum;    while(scanf("%lf%lf",&temp1,&temp2)!=EOF){        if(temp1==-1&&temp2==-1)            {                cnt++;                continue;            }        else        {            stop[i].x =temp1;            stop[i++].y =temp2;            line[cnt]++;            stationnum=i;        }    }        for(int j=1;j<cnt;j++)            line[j]=line[j]+line[j-1];        for(i=2;i<cnt;i++)            for(int j=line[i]+1;j<line[i+1];j++)                map[j][j+1]=map[j+1][j]=sqrt((double)((stop[j].x-stop[j+1].x)*(stop[j].x-stop[j+1].x)+(stop[j].y-stop[j+1].y)*(stop[j].y-stop[j+1].y)))/V2;        for(int j=1;j<=stationnum;j++)            for(int k=j+1;k<=stationnum;k++)            {                if(map[j][k]>0xfffff)                       map[j][k]=map[k][j]=sqrt((double)((stop[j].x-stop[k].x)*(stop[j].x-stop[k].x)+(stop[j].y-stop[k].y)*(stop[j].y-stop[k].y)))/V1;            }           /*for(int i=1;i<stationnum;i++)        {                for(int j=1;j<stationnum;j++)                printf("%f         ",map[i][j]);                printf("\n");        }*/        for(int k=1;k<stationnum;k++)            for(int i=1;i<stationnum;i++)                for(int j=1;j<stationnum;j++)                {                    if(map[i][j]>=map[i][k]+map[k][j])                        map[i][j]=map[i][k]+map[k][j];                }    printf("%.0lf\n",map[1][2]);}
原创粉丝点击