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;
const int maxn=310;
const int inf=1e9;
struct nod
{
    double x,y;
}node[maxn];
double cost[maxn][maxn],dis[maxn];
bool vis[maxn];
void dij(int n,int u)
{
    for(int i=2;i<=n;i++)
    {
        dis[i]=inf;
    }
    int start=u;
    vis[1]=1;
        for(int i=1;i<=n;i++)
        {
          dis[i]=(cost[start][i]<=dis[i])?cost[start][i]:dis[i];
        }
    // printf("%d\n",start);
    for(int i=1;i<=n-1;i++)
    { //找到理起始点最近的点
     int min=9999999;
     for(int i=1;i<=n;i++)
     {

                     if(min>=dis[i]&&vis[i]==0)
            {
                min=dis[i];
                 start=i;
            }
     }
     vis[start]=1;

            for(int i=1;i<=n;i++)
    {
        if(vis[i]!=1)
        dis[i]= (dis[i]<=(dis[start]+cost[start][i]))?dis[i]:(dis[start]+cost[start][i]);
    }
    }

}
double dist(nod a,nod b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{
    double v1=10000.0/60;
    double v2=40000.0/60;
    scanf("%lf%lf%lf%lf",&node[1].x,&node[1].y,&node[2].x,&node[2].y);
    int n=2;
    int cnt1=3;
    for(int i=1;i<=300;i++)
    {
        for(int j=1;j<=300;j++)
        {
            if(i==j) cost[i][j]=0;
            else cost[i][j]=inf;
        }
    }
    double x,y;
    while(scanf("%lf%lf",&x,&y))
    {
        if(x==-1&&y==-1)
        {
            cnt1=n+1;
            break;
        }
        n++;
        node[n].x=x;
        node[n].y=y;
        if(n!=cnt1) cost[n][n-1]=cost[n-1][n]=min(cost[n][n-1],dist(node[n],node[n-1])/v2);
    }
    while(scanf("%lf%lf",&x,&y)!=EOF)
    {
        n++;
        node[n].x=x;
        node[n].y=y;
        while(scanf("%lf%lf",&x,&y)!=EOF)
        {
            if(x==-1&&y==-1) break;
                n++;
                node[n].x=x;
                node[n].y=y;
        }
        if(n!=cnt1) cost[n][n-1]=cost[n-1][n]=min(cost[n][n-1],dist(node[n],node[n-1])/v2);
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            cost[i][j]=min(cost[i][j],dist(node[i],node[j])/v1);
        }
    }
    dij(n,1);
    printf("%d\n",(int)(dis[2]+0.5));
    return 0;
}

原文地址:https://www.cnblogs.com/tombraider-shadow/p/11184054.html

时间: 2024-10-11 11:12:57

POJ 2502 SUBWAY(最短路)的相关文章

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(迪杰斯特拉)

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 题目大意: 一个学生去上学,从家到学校,可以有若干个地铁路线,每个地铁路线有若干站,给出步行和地铁的速度,问:最短用多长时间从家到达学校? 解题思路: 有地铁的两点建立地铁路线,没有的步行,求最短路,但是有一点坑的是地铁线有可能是曲线,也就是说从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

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

POJ 2449Remmarguts&#39; Date K短路模板 A*+SPFA

太水了我不想说了,模板在这里 14312K 313MS 1 #include<queue> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int v[100010],v2[100010],c[100010],c2[100010],s,t,k,duin; 7 int n,m,point[1010],next[100010],cnt=0,

[有向树的最小表示] poj 1635 Subway tree systems

题目链接: http://poj.org/problem?id=1635 Subway tree systems Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6541   Accepted: 2747 Description Some major cities have subway systems in the form of a tree, i.e. between any pair of stations, th

POJ训练计划2253_Frogger(最短路/floyd)

解题报告 题意: 求0到1所有路中最大值最小的那个数. 思路: floyd. #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #define inf 0x3f3f3f3f using namespace std; int n,m,q; double mmap[210][210]; struct node { double x,y; } p[210]; doub