当前位置 : 主页 > 编程语言 > c语言 >

POJ 2502 Subway(最短路)

来源:互联网 收集:自由互联 发布时间:2023-09-07
Subway Time Limit:1000MS Memory Limit:65536K Total Submissions:6229 Accepted:2026 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 ge


Subway


Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 6229

 

Accepted: 2026


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





          题目大意:一个人从家要到学校去,途中有许多车站,所以有步行和做地铁两种方式,其速度分别是10km/h 和40km/h。输入的规则是第一行输入的是x1,y1,x2,y2,分别代表家的坐标和学校的坐标。以后输入的是车站的坐标,数目不超过200,相邻的两个站点可以坐地铁,其他的需要步行。问到达学校的最短时间是多少?


         因为不知道输入的数据有多少,所以用while(scanf()!=EOF)。其他的就没有什么要注意的了,建图很重要。





#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<math.h>

using namespace std;
const int N = 300;
const int INF = 9999999;

double map[N][N];
double num[N];
int v[N];
int n,m;
struct node
{
    int x,y;
}q[10001];
void dijkstra()
{
    for(int i=0;i<n;i++)
    {
        num[i] = map[0][i];
        v[i] = 0;
    }
    num[0] = 0;
    for(int i=0;i<n;i++)
    {
        int min = INF,k;
        for(int j=0;j<n;j++)
        {
            if(v[j] == 0 && num[j]<min)
            {
                min = num[j];
                k = j;
            }
        }
        if(min == INF)
        {
            break;
        }
        v[k] = 1;
        for(int j=0;j<n;j++)
        {
            if(v[j] == 0 && num[j] > num[k] + map[k][j])
            {
                num[j] = num[k] + map[j][k];
            }
        }
    }
    printf("%.0lf\n",num[1]);
}

int main()
{
    while(cin >> q[0].x >> q[0].y >> q[1].x >> q[1].y)
    {
        for(int i=0;i<N;i++)
        {
            for(int j=0;j<=N;j++)
            {
                map[i][j] = INF;
            }
            map[i][i] = 0;
        }
        n = m = 2;
        while(cin >> q[n].x >> q[n].y)
        {
            if(q[n].x == -1 && q[n].y == -1)
            {
                m = n;
                continue;
            }
            if(n!=m)
            {
                double d = sqrt((double)(q[n].y-q[n-1].y)*(q[n].y - q[n-1].y) + (q[n].x - q[n-1].x)*(q[n].x - q[n-1].x));
                d = 3*d/2000;
                map[n][n-1] = d;
                map[n-1][n] = d;
            }
            n++;
        }
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<=n;j++)
            {
                if(map[i][j] == INF)
                {
                    double d = sqrt((double)(q[j].y-q[i].y)*(q[j].y - q[i].y) + (q[j].x - q[i].x)*(q[j].x - q[i].x));
                    d = 3*d/500;
                    map[i][j] = d;
                    map[j][i] = d;
                }
            }
        }
        dijkstra();
    }
    return 0;
}



【转自:香港服务器 https://www.68idc.cn提供,感谢支持】
上一篇:HDU1710 Binary Tree Traversals
下一篇:没有了
网友评论