Subway poj 2502

http://poj.org/problem?id=2502

题意:在一个城市里,分布着若干条地铁线路,每条地铁线路有若干个站点,所有地铁的速度均为40km/h。现在你知道了出发地和终点的坐标,以及这些地铁 线路每个站点的坐标,你的步行速度为10km/h,且你到了地铁的任意一个站之后就刚好有地铁出发。问你从出发点到终点最少需要多少时间。

#include<stdio.h>
#include<vector>
#include<queue>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;

const int maxn = 100005;
const int oo = 0xfffffff;
const double vs = 40*1000/60.0;//每分钟走多少米
const double vp = 10*1000/60.0;
int head[maxn], v[maxn];
double dist[maxn];
int cnt;

struct node
{
    int u, v, next;
    double w;
}maps[maxn];

struct point
{
    int x,y;
}s[maxn];

void Add(int u, int v, double w)
{
    maps[cnt].v = v;
    maps[cnt].w = w;
    maps[cnt].next = head[u];
    head[u] = cnt ++;
}

double Len(point p , point q, double t)
{
      return sqrt((p.x-q.x)*(p.x-q.x)*1.0+(p.y-q.y)*(p.y-q.y))/t;
}

void spfa()
{
    queue<int>Q;
    Q.push(1);
    v[1]=1;

    while(Q.size())
    {
        int p=Q.front();
        Q.pop();
        v[p]=0;

        for(int i=head[p]; i!=-1; i=maps[i].next)
        {
            int q=maps[i].v;

            if(dist[q]>dist[p]+maps[i].w)
            {
                dist[q]=dist[p]+maps[i].w;
                if(!v[q])
                {
                    v[q]=1;
                    Q.push(q);
                }
            }
        }
    }
}

int main()
{
    int flag = 0;
    scanf("%d %d %d %d",&s[1].x, &s[1].y, &s[2].x, &s[2].y);

    int k = 3;

    cnt = 0;
    memset(head, -1, sizeof(head));

    while(scanf("%d %d", &s[k].x, &s[k].y)!=EOF)
    {
        if(s[k].x != -1)
        {
            if( !flag )
             flag = 1;
             else
             {
                 double sl = Len(s[k], s[k-1], vs);

                 Add(k, k-1, sl);
                 Add(k-1, k, sl);
             }

             k++;

        }
        else
            flag = 0;
    }

    for(int i=1; i<k; i++)
    {
        for(int j=i+1; j<=k; j++)
        {
            double sl = Len(s[i], s[j], vp);

            Add(i, j, sl);
            Add(j, i, sl);
        }
    }

    for(int i=1; i<=k; i++)
        dist[i] = oo;
        dist[1] = 0;

   spfa();

   printf("%.0f\n", dist[2]);

    return 0;
}

时间: 2024-10-06 00:10:49

Subway poj 2502的相关文章

L - Subway - POJ 2502

题意:在一个城市里,分布着若干条地铁线路,每条地铁线路有若干个站点,所有地铁的速度均为40km/h.现在你知道了出发地和终点的坐标,以及这些地铁 线路每个站点的坐标,你的步行速度为10km/h,且你到了地铁的任意一个站之后就刚好有地铁出发.问你从出发点到终点最少需要多少时间. ////////////////////////////////////////////////////////////////////// 有很多站点,给的数据只是一条条线路,所以需要先预处理数据,增加任意两点人走需要的

Subway POJ - 2502 最短路

题意:给出地铁线  起点和 终点  坐地铁速度为v2  走路为v1 求起点到终点的最短距离  (答案需要四舍五入这里坑了好久) 拿给出的地铁站点 和起点终点建边即可  然后跑个迪杰斯特拉 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 const double v1=10000.0/60; 7 cons

Subway POJ - 2502 spfa

#include<cstdio> #include<cmath> #include<cstring> #include<cstring> #include<iostream> #include<queue> using namespace std; const int N=205; const double INF=0x3f3f3f3f; int k,l; struct node { double x,y; int num; } s[

POJ 2502 SUBWAY(最短路)

POJ 2502 SUBWAY 题目链接:http://poj.org/problem?id=2502 题目大意:求从a点到b点所需要的最短时间. 题目思路:用最短路来求,把各个点之间的时间看作所需要的路程.然后用 dij求最短路就可以了,感觉输入有点坑,还有在每条地铁线上,只有相同地铁线上的 点可以互相到达. #include<stdio.h> #include<algorithm> #include<math.h> using namespace std; cons

POJ 2502 Subway(迪杰斯特拉)

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

POJ 2502 Subway(最短路径)

原题地址:http://poj.org/problem?id=2502 Subway Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7347   Accepted: 2387 Description You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of getting to ride your bi

POJ 2502 Subway (Dijkstra 最短路+建图)

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

poj 2502 Subway

题目链接: http://poj.org/problem?id=2502 题目大意: 一个学生去上学,从家到学校,可以有若干个地铁路线,每个地铁路线有若干站,给出步行和地铁的速度,问:最短用多长时间从家到达学校? 解题思路: 有地铁的两点建立地铁路线,没有的步行,求最短路,但是有一点坑的是地铁线有可能是曲线,也就是说从a站到b站再到c站的距离和大于a站直接到c站的距离 1 #include <cstdio> 2 #include <cstring> 3 #include <c

(简单) POJ 2502 Subway,Dijkstra。

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