Floyd算法  POJ2502

Subway
Time Limit: 1000MS

Memory Limit: 65536K
Total Submissions: 11572

Accepted: 3775
Description
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 1000
0 200 5000 200 7000 200 -1 -1 
2000 600 5000 600 10000 600 -1 -1
Sample Output
21
Source
Waterloo 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 0xfffffff
double 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]);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值