POJ2502 Subway(最短路Floyed)

Subway
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6539 Accepted: 2129

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

解题思路:这题主要是怎么存储这些点,数据量比较小,用floyed和dijkstra都可以,题意就是给出起点和终点的坐标,然后给出多条地铁线路,告诉你地铁速度和步行的速度,然后问你怎么走最省时间。存储这些点的时候实际上是给这些点标号,每个标号存的信息就是这个点的坐标,然后在同一条线路上的只要更新同一条线路上相邻两个点就可以了,不相邻的点在求最短路时候会计算出。


这题wa了好久,原因是输入我原来是用的整型,可能有误差吧。坑。。。。另外就是用C++提交是RE不知道为什么。

/********************************/
/*Problem:		POJ2502	        */
/*User: 	    shinelin	    */
/*Memory: 	    1060K		    */
/*Time: 	    188MS		    */
/*Language: 	g++		        */
/********************************/
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <cstring>
#include <string>
#include <list>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
using namespace std;

#define INF 0xfffffff
#define LL long long
#define MAXN 205

struct point
{
    double x;
    double y;
    point() {}
    point(double a, double b)
    {
        x = a;
        y = b;
    }
};

vector<point> node;
double Time[MAXN][MAXN];


double tran(int i, int j, double speed)
{
    return sqrt((node[i].x - node[j].x)*(node[i].x - node[j].x)
    + (node[i].y - node[j].y) * (node[i].y - node[j].y)) * 60.0 / (speed * 1000);
}

int main()
{
    double hx, hy, sx, sy;
    point p;
    //initial
    for(int i = 1; i <= MAXN; i ++)
    {
        for(int j = i +1; j <= MAXN; j ++)
        {
            Time[i][j] = Time[j][i] = INF;

        }
        Time[i][i] = 0;
    }

    scanf("%lf%lf%lf%lf", &hx, &hy, &sx, &sy);
    node.push_back(point(hx, hy));
    node.push_back(point(sx, sy));
    Time[1][2] = Time[2][1] = tran(0, 1, 10.0);
    int i = 2;
    bool flag = 0;
    while(scanf("%lf%lf", &p.x, &p.y) != EOF)
    {
        if(p.x + p.y < 0)
        {
            flag = 0;
            continue;
        }
        i ++;
        node.push_back(p);
        for (int k = 1; k < i; k ++)
        {
            Time[k][i] = Time[i][k] = min(tran(k - 1, i - 1, 10.0), Time[k][i]);
        }
        if(flag)
            Time[i][i-1] = Time[i-1][i] = min(tran(i - 2, i - 1, 40.0), Time[i][i-1]);
        flag = 1;
    }
    int pnum = node.size();
    for (int k = 1; k <= pnum; k ++)
    {
        for (int i = 1; i <= pnum; i ++)
        {
            for (int j = 1; j <= pnum; j ++)
            {
                if(Time[i][k] + Time[k][j] < Time[i][j])
                {
                    Time[i][j] = Time[i][k] + Time[k][j];
                }
            }
        }
    }
    printf("%0.0f\n", Time[1][2]);
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值