• POJ 2502 Subway(最短路)


    Subway
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 6979   Accepted: 2287

    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 how long it will take you to get to school. 
    You walk at a speed of 10 km/h. The subway travels at 40 km/h. Assume that you are lucky, and whenever you arrive at a subway station, a train is there that you can board immediately. You may get on and off the subway any number of times, and you may switch between different subway lines if you wish. All subway lines go in both directions.

    Input

    Input consists of the x,y coordinates of your home and your school, followed by specifications of several subway lines. Each subway line consists of the non-negative integer x,y coordinates of each stop on the line, in order. You may assume the subway runs in a straight line between adjacent stops, and the coordinates represent an integral number of metres. Each line has at least two stops. The end of each subway line is followed by the dummy coordinate pair -1,-1. In total there are at most 200 subway stops in the city.

    Output

    Output is the number of minutes it will take you to get to school, rounded to the nearest minute, taking the fastest route.

    Sample Input

    0 0 10000 1000
    0 200 5000 200 7000 200 -1 -1 
    2000 600 5000 600 10000 600 -1 -1

    Sample Output

    21
    

    Source

     
    题意:给定起点和终点的坐标和每条地铁线路的每个地铁站的坐标,求从起点到终点最短用了多少分钟?(走路10km/h,坐地铁40km/h)
    分析:建好图用最短路,要注意单位,如果两点之间有地铁的话,那么做地铁要比走路快(常识ORZ)没有地铁线路的话就要计算出走路用的时间。
    #include<cstdio>
    #include<string>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<stack>
    #include<queue>
    #include<map>
    #include<stdlib.h>
    #include<algorithm>
    #define LL __int64
    using namespace std;
    const int MAXN=300+5;
    const double INF=0x3f3f3f3f ;
    
    struct node
    {
        double x,y;
    }po[MAXN];
    double w[MAXN][MAXN],d[MAXN];
    int vis[MAXN];
    int tot;
    double calc(node A,node B)
    {
        return sqrt( (A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y) );
    }
    void dij()
    {
    //    memset(vis,0,sizeof(vis));
    //    memset(d,INF,sizeof(INF))
        for(int i=1;i<=tot;i++)
        {
            vis[i]=0;
            d[i]=INF;
        }
        d[1]=0;
    
        for(int i=1;i<=tot;i++)
        {
            int tmp;
            double minn=INF;
            for(int j=1;j<=tot;j++)
            {
                if(!vis[j] && d[j]<minn)
                {
                    minn=d[j];
                    tmp=j;
                }
            }
            vis[tmp]=1;
            for(int j=1;j<=tot;j++)
                if(d[tmp]+w[tmp][j]<d[j])
                    d[j]=d[tmp]+w[tmp][j];
        }
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        scanf("%lf %lf %lf %lf",&po[1].x,&po[1].y,&po[2].x,&po[2].y);
    
        for(int i=1;i<=MAXN;i++)
            for(int j=1;j<=MAXN;j++)
            {
                if(i==j) w[i][j]=0;
                else w[i][j]=INF;
            }
    
        bool flag=false;
        double x,y;
        tot=3;
        while(scanf("%lf %lf",&x,&y)==2)
        {
            if(x==-1 && y==-1)
            {
                flag=false;
                continue;
            }
            po[tot].x=x;
            po[tot].y=y;
            if(flag && calc(po[tot-1],po[tot])/40000.0<w[tot][tot-1])
                w[tot][tot-1]=w[tot-1][tot]=calc(po[tot-1],po[tot])/40000.0;
            tot++;
            flag=true;
        }
        for(int i=1;i<=tot;i++)
            for(int j=1;j<=tot;j++)
                if(i!=j && w[i][j]==INF)
                    w[i][j]=calc(po[i],po[j])/10000.0;
        dij();
    
        printf("%d
    ",(int)(d[2]*60+0.5));
    
        return 0;
    }
    View Code
  • 相关阅读:
    [LeetCode] Contains Duplicate 包含重复值
    [LeetCode] 281. The Skyline Problem 天际线问题
    Qt resizeEvent 控件居中设置
    [LeetCode] 214. Shortest Palindrome 最短回文串
    Qt Creator Shortcuts 快捷键大全
    Qt SizePolicy 属性
    [LeetCode] 30. Substring with Concatenation of All Words 串联所有单词的子串
    [LeetCode] 213. House Robber II 打家劫舍之二
    [LeetCode] 212. Word Search II 词语搜索之二
    [LeetCode] 18. 4Sum 四数之和
  • 原文地址:https://www.cnblogs.com/clliff/p/4680312.html
Copyright © 2020-2023  润新知