Dijkstra-POJ-2502-Subway

来源:互联网 发布:上海ug编程培训 编辑:程序博客网 时间:2024/05/16 05:29

Subway
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7504 Accepted: 2440
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

刷这一道的真的是让人很不开心。。。首先,输入非常蛋疼;其次,输出要四舍五入,而题目乱七八糟并没有说清楚;最后,G++来编译居然又WA而C++AC,查了查说是编译器精度缺损的问题。
题目大致处理方法就是将每个地铁站点与相邻的站点用时先算出,然后再算各个点之间步行耗时,最后一个dijkstra。
总之一道很简单的最短路题刷起来却非常揪心,在我看来并不算一道好题。

////  main.cpp//  最短路练习-L-Subway////  Created by 袁子涵 on 15/10/17.//  Copyright © 2015年 袁子涵. All rights reserved.//#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <cmath>#define Maxin 205#define INF 0xFFFFFFFF#define minof(a,b)  ((a)>(b)?(b):(a))using namespace std;typedef struct coor{    long long int x,y;}Coor;Coor start,fin,node[Maxin];double dis[Maxin],map[Maxin][Maxin];bool visit[Maxin];int n=2;double v1=40000.0/60,v2=10000.0/60;double distances(Coor a,Coor b){    return sqrt((double)(a.x-b.x)*(a.x-b.x)+(double)(a.y-b.y)*(a.y-b.y));}void dijkstra(){    double mins;    int next=-1;    for (int i=0; i<n; i++) {        dis[i]=map[0][i];    }    visit[0]=1;    for (int i=0; i<n; i++) {        mins=INF;        for (int j=0; j<n; j++) {            if (visit[j]==0 && mins>dis[j]) {                mins=dis[j];                next=j;            }        }        if (next==-1) {            break;        }        visit[next]=1;        for (int j=0; j<n; j++) {            if (dis[j]>map[next][j]+dis[next] && visit[j]==0) {                dis[j]=map[next][j]+dis[next];            }        }    }}int main(int argc, const char * argv[]) {    long long int x,y;    bool flag=0;    cin >> start.x >> start.y >> fin.x >> fin.y;    node[0]=start;    node[1]=fin;    memset(visit, 0, sizeof(visit));    memset(map, 0, sizeof(map));    for (int i=0; i<Maxin; i++) {        for (int j=0; j<Maxin; j++) {            map[i][j]=INF;        }        dis[i]=INF;        map[i][i]=0;    }    dis[0]=0;    visit[0]=1;    while (scanf("%lld%lld",&x,&y)==2) {        if (x==-1 && y==-1) {            flag=0;            continue;        }        node[n].x=x;        node[n].y=y;        if (flag==1) {            map[n][n-1]=map[n-1][n]=minof(distances(node[n], node[n-1])/v1,map[n][n-1]);        }        flag=1;        n++;    }    for (int i=0; i<n; i++) {        for (int j=0; j<n; j++) {            map[i][j]=map[j][i]=minof(distances(node[i], node[j])/v2, map[i][j]);        }    }    dijkstra();    printf("%.0lf\n",dis[1]);    return 0;}
0 0