Subway---poj2502(最短路)

题目链接:http://poj.org/problem?id=2502

人走路的速度是10km/h,地铁的速度是40km/h题目给出一个起点,一个终点,以及几条地铁线路运行的站点。题目给的点的做坐标单位是m;答案输出从起点到终点的最短时间(分钟数)。

 

10km/h= 10000/60 m/min -----6/1000 min/m;

40km/h= 40000/60 m/min------3/2000 min/m;

 

所有点之间都可以步行到达,每条地铁的相邻两站可以相互到达,其他的都不可到达;

求起点到终点的最短路即可;

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <queue>
#include <stack>
#include <algorithm>
#include <map>
#include <string>
typedef long long LL;
#define INF 1e30
#define met(a, b) memset(a, b, sizeof(a))
#define N 515

using namespace std;

struct node
{
    double x,y;
}a[N];

double G[N][N], dist[N];
int vis[N], cnt;

double Dij(int s)
{
    met(vis, 0);
    for(int i=1; i<=cnt; i++)
        dist[i] = INF;
    dist[s] = 0;
    for(int i=1; i<cnt; i++)
    {
        double Min = INF; int Index = -1;
        for(int j=1; j<cnt; j++)
        {
            if(!vis[j] && Min>dist[j])
            {
                Min = dist[j];
                Index = j;
            }
        }
        if(Index == -1)break;
        vis[Index] = 1;
        for(int j=1; j<cnt; j++)
        {
            if(!vis[j] && dist[j]>dist[Index]+G[Index][j])
            {
                dist[j] = dist[Index]+G[Index][j];
            }
        }
    }
    return dist[2];
}

int main()
{
    while(scanf("%lf %lf %lf %lf", &a[1].x, &a[1].y, &a[2].x, &a[2].y)!=EOF)
    {
        for(int i=1; i<=300; i++)
        {
            for(int j=1; j<=300; j++)
                G[i][j] = INF;
            G[i][i] = 0;
        }
        cnt = 3; int pre = -1; double x, y;
        while(scanf("%lf %lf", &x, &y) != EOF)
        {
            if(x == -1 && y == -1)
            {
                pre = -1;
                continue;
            }
            a[cnt].x = x, a[cnt].y = y;
            if(pre!=-1)
            {
                double d = sqrt((a[pre].x-x)*(a[pre].x-x)+(a[pre].y-y)*(a[pre].y-y));
                G[pre][cnt] = G[cnt][pre] = min(G[cnt][pre], d*3.0/2000);
            }
            pre = cnt++;
        }

        for(int i=1; i<cnt; i++)
        {
            for(int j=1; j<cnt; j++)
            {
                double d = sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y));
                G[i][j] = G[j][i] = min(G[i][j],d*6.0/1000);
            }
        }

        double ans = Dij(1);
        printf("%.0f\n", ans);
    }
    return 0;
}

时间: 2024-07-28 15:00:47

Subway---poj2502(最短路)的相关文章

AtCoder ARC061E Snuke&#39;s Subway Trip 最短路

目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:传送门 ?Portal ?原题目描述在最下面. ?\(n(1e5)\)个点, \(m(2e5)\)条边, 每条边有一个属性值.经过一条同一属性值的连续路径花费为1.问从1到n的最小花费. Solution: 我的解法 ?直接边最短路搞,然后t的飞起.仔细一想,这样写的话有\(1e5\)个点,边数更是多到飞起,拿命跑啊,不过代码我还是放下面. 正解:拆点 ?把一条\(u

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

poj2502 最短路

1 //Accepted 504 KB 16 ms 2 //spfa最短路 3 //把n个地铁站作为n个顶点,边权为从一个站到另一个站的时间 4 //注意:地铁在相邻的两站之间是直线行驶,但其他的就不是了 5 #include <cstdio> 6 #include <cstring> 7 #include <iostream> 8 #include <queue> 9 #include <cmath> 10 #include <algor

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

poj2502(最短路)

题目连接:http://poj.org/problem?id=2502 用模拟栈试试... 1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<stack> //用了模拟栈 5 using namespace std; 6 const int maxn=210; 7 const int inf=0x3f3f3f3f; 8 double x[maxn],y[maxn],dis[

poj2502题解(建图+最短路)

题意 给起点和终点的坐标,然后给出多条地铁每一站的坐标,每站地铁只能到相邻的地铁站,地铁的速度是40km/h,人行走的速度是10km/h,求起点到终点的最小时间(给出的坐标单位是m,最后求的时间单位是分钟) 解题思路 这题关键点在与建图,把图建好后跑dijkstra就很简单了,之前一直wa是想复杂了,以为要考虑起点和终点在地铁站的情况和不同线路的地铁站相交的情况,后来想不需要这么麻烦,同一条线路相邻的40,不同的10就可以了,相交的话,以10km/h走时间花费是0. AC代码 #include<

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-经过预处理的最短路

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

多源最短路

Stockbroker Grapevine http://poj.org/problem?id=1125 模板题 1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 const int inf=0x3f3f3ff; 5 const int M=128; 6 class Floyd{///多源最短路o(MV^3) 7 typedef int typec;///边权的类型 8 static const in

[kuangbin带你飞]专题四 最短路练习

A. POJ 2387  Til the Cows Come Home 模板题. #include<iostream> #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #include<vector> #include<string> #include<map> #include<queue> #in