ZOJ1891 Subway【Dijkstra算法】

Subway
Time Limit: 5000 ms Memory Limit: 32768 KB

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. A pair of -1 indicates the end of the subway list.

Input contains multiple test cases. Process to the end of file.

Output

Output for each test case 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
-1 -1

Sample Output

21

问题链接ZOJ1891 Subway
问题简述:(略)
问题分析:最短路问题,用Dijkstra算法解决。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* ZOJ1891 Subway */

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>

using namespace std;

const int N = 200 + 5;
struct Point {
    int x, y, id;
} p[N];

double getdis(int u, int v)
{
    double t1 = (double)(p[u].x - p[v].x) * (p[u].x - p[v].x);
    double t2 = (double)(p[u].y - p[v].y) * (p[u].y - p[v].y);
    if(p[u].id != p[v].id || (u != v - 1 && v != u - 1))
        return sqrt(t1 + t2) / 10000.0 * 60.0;
    else
        return sqrt(t1 + t2) / 40000.0 * 60.0;
}

/* Dijkstra算法
 * 复杂度:O(N×N)
 * 输入:n 全局变量,图结点数
 *      g 全局变量,邻接矩阵,g[i][j]表示结点i到j间的边距离(这里用函数计算)
 * 输出:dis 全局变量,dis[i]表示结点1到i的最短距离
 */
const int INF = 0x3f3f3f3f;
//const int N = 200 + 1;
double dis[N];
int n, vis[N];

void dijkstra()
{
    memset(vis, 0, sizeof(vis));
    vis[1] = 1;

    for(int i = 2; i <= n ;i++) dis[i] = getdis(0, i);

    for(int i = 2; i <= n; i++) {
        double mindis = INF;
        int u;
        for(int j = 2; j <= n; j++)
            if( vis[j]== 0 && dis[j] < mindis )
                mindis = dis[j], u = j;
        vis[u] = 1, dis[u] = mindis;

        for(int v = 2; v <= n; v++)
            if( vis[v] == 0)
                dis[v] = min(dis[v], dis[u] + getdis(u, v));
    }
}

int main()
{
    while(~scanf("%d%d", &p[1].x, &p[1].y) && (p[1].x != -1 || p[1].y != -1)) {
        scanf("%d%d", &p[2].x, &p[2].y);
        p[1].id = 1, p[2].id = 2;
        int id = 3;
        n = 3;
        while(~scanf("%d%d", &p[n].x, &p[n].y) && (p[n].x != -1 || p[n].y != -1)) {
            p[n++].id = id;
            while(~scanf("%d%d", &p[n].x, &p[n].y) && (p[n].x != -1 || p[n].y != -1))
                p[n++].id = id;
            id++;
        }
        n--;

        dijkstra();

        printf("%.0lf\n", dis[2]);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值