L - Subway - POJ 2502

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

//////////////////////////////////////////////////////////////////////

有很多站点,给的数据只是一条条线路,所以需要先预处理数据,增加任意两点人走需要的时间。数据预先处理比较麻烦......

#include<stdio.h>
#include<vector>
#include<stack>
#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;

struct node
{
    int u, v, next;
    double c;//两点之间所需时间
}e[maxn];
struct point
{
    int x, y;
}p[1005];

int nPoint, head[1005];
double dis[1005];
bool vis[1005];

double Len(point a, point b, double v)
{
    return sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) ) / v;
}
void Add(int u, int v, double len, int k)
{
    e[k].u = u;
    e[k].v = v;
    e[k].c = len;
    e[k].next = head[u];
    head[u] = k;
}
void spfa()
{
    stack<int> sta;
    sta.push(1);

while(sta.size())
    {
        int i = sta.top();sta.pop();
        vis[i] = false;

for(int j=head[i]; j!=0; j=e[j].next)
        {
            int u = e[j].u, v = e[j].v;
            double c = e[j].c;

if(dis[u]+c < dis[v])
            {
                dis[v] = dis[u] + c;
                if(vis[v] == false)
                {
                    vis[v] = true;
                    sta.push(v);
                }
            }
        }
    }
}

int main()
{
    int i, j, flag=0, k=1;

scanf("%d%d%d%d", &p[1].x, &p[1].y, &p[2].x, &p[2].y);

nPoint = 3;
    while(scanf("%d%d", &p[nPoint].x, &p[nPoint].y) != EOF)
    {
        if(p[nPoint].x != -1)
        {
            if(flag == 0)
                flag = 1;
            else
            {
                double c = Len(p[nPoint], p[nPoint-1], vs);
                Add(nPoint, nPoint-1, c, k++);
                Add(nPoint-1, nPoint, c, k++);
            }

nPoint++;
        }
        else
            flag = 0;
    }

for(i=1; i<nPoint; i++)
    for(j=i+1; j<=nPoint; j++)
    {
        double c = Len(p[i], p[j], vp);
        Add(i, j, c, k++);
        Add(j, i, c, k++);
    }

for(i=2; i<nPoint; i++)
        dis[i] = oo;

spfa();

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

return 0;
}

时间: 2024-07-29 04:55:06

L - Subway - POJ 2502的相关文章

Subway poj 2502

http://poj.org/problem?id=2502 题意:在一个城市里,分布着若干条地铁线路,每条地铁线路有若干个站点,所有地铁的速度均为40km/h.现在你知道了出发地和终点的坐标,以及这些地铁 线路每个站点的坐标,你的步行速度为10km/h,且你到了地铁的任意一个站之后就刚好有地铁出发.问你从出发点到终点最少需要多少时间. #include<stdio.h> #include<vector> #include<queue> #include<algo

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[

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

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

Dijkstra+计算几何 POJ 2502 Subway

题目传送门 题意:列车上行驶40, 其余走路速度10.问从家到学校的最短时间 分析:关键是建图:相邻站点的速度是40,否则都可以走路10的速度.读入数据也很变态. #include <cstdio> #include <cmath> #include <algorithm> #include <cstring> #include <queue> using namespace std; const int N = 2e2 + 5; const i