POJ-2502-Subway

链接:https://vjudge.net/problem/POJ-2502

题意:

一个人从家要到学校去,途中有许多车站,所以有步行和做地铁两种方式,其速度分别是10km/h 和40km/h。输入的规则是第一行输入的是x1,y1,x2,y2,分别代表家的坐标和学校的坐标。以后输入的是车站的坐标,数目不超过200,相邻的两个站点可以坐地铁,其他的需要步行。问到达学校的最短时间是多少?(因为不知道输入的数据有多少,所以用while(scanf()!=EOF)。其他的就没有什么要注意的了,建图很重要。)

思路:

建图,注意地铁只有一条线上两两相邻的站才能乘坐。。。

Dijkstra即可。

代码:

#include <iostream>
#include <memory.h>
#include <string>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
#include <math.h>
using namespace std;
const int MAXN = 405;
const int INF = 1e9;
struct Node
{
    double x,y;
}node[MAXN];
double Map[MAXN][MAXN];
double Dis[MAXN];
int Vis[MAXN];
int cnt = 0;

double Get_Len(double x1, double y1, double x2, double y2)
{
    return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
}

double Dijkstra()
{
    for (int i = 1;i < cnt ;i++)
        Dis[i] = Map[1][i];
    Vis[1] = 1;
    for (int i = 1;i < cnt ;i++)
    {
        double now = INF;
        int w = -1;
        for (int j = 1;j < cnt ;j++)
        {
            if (Vis[j] == 0&&now > Dis[j])
            {
                w = j;
                now = Dis[j];
            }
        }
        if (w == 2)
            return Dis[2];
        Vis[w] = 1;
        for (int j = 1;j < cnt ;j++)
        {
            if (Vis[j] == 0&&Dis[j] > Dis[w] + Map[w][j])
            {
                Dis[j] = Dis[w] + Map[w][j];
            }
        }
    }
}

int main()
{
    scanf("%lf%lf%lf%lf",&node[1].x,&node[1].y,&node[2].x,&node[2].y);
    double x,y;
    int tmp = 3;
    cnt = 3;
    while (~scanf("%lf%lf",&x,&y))
    //while (scanf("%lf%lf",&x,&y)&&x >= -1)
    {
        if (x == -1&&y == -1)
        {
            for (int i = tmp;i<cnt-1;i++)
            {
                double len = Get_Len(node[i].x,node[i].y,node[i+1].x,node[i+1].y) / 40000.0;
                Map[i][i+1] = Map[i+1][i] = len;
            }
            tmp = cnt;
        }
        else
        {
            node[cnt].x = x;
            node[cnt++].y = y;
        }
    }
    for (int i = 1;i < cnt;i++)
        for (int j = 1;j < cnt ;j++)
            if (i == j)
                Map[i][j] = 0;
            else if (Map[i][j] == 0)
                Map[i][j] = Get_Len(node[i].x,node[i].y,node[j].x,node[j].y) / 10000.0;
    printf("%d\n",(int)(Dijkstra()*60.0+0.5));

    return 0;
}

  

posted @ 2019-01-16 21:04  YDDDD  阅读(178)  评论(0编辑  收藏  举报