zoukankan      html  css  js  c++  java
  • poj2502最短路!

    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
    
    此题没输出,真是蛋疼,错了也不知道怎么改,最后还是参考得来,因为范围问题错了n次,最关键的地方就是储存图!!!
    处理时很要注重细节
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cmath>
    #include<queue>
    using namespace std;
    const double inf=1e30;
    double d[300],cost[300][300];
    int n=2;
    struct node{
       double x,y;
    }e[300];
    double dis(node a,node b)
    {
        return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    }
    void dijkstra()
    {
        queue<int>Q;
        bool vis[300];
        for(int i=0;i<n;i++)
        {
            d[i]=inf;
            vis[i]=0;
        }
        d[0]=0;
        Q.push(0);
        vis[0]=1;
        while(!Q.empty()){
            int a=Q.front();
            Q.pop();
            vis[a]=0;
            for(int i=0;i<n;i++)
                if(d[i]>d[a]+cost[a][i])
                {
                    d[i]=d[a]+cost[a][i];
                    if(!vis[i])
                    {
                        vis[i]=1;
                        Q.push(i);
                    }
                }
        }
    }
    int main()
    {
        scanf("%lf%lf%lf%lf",&e[0].x,&e[0].y,&e[1].x,&e[1].y);
        for(int i=0;i<300;i++)
            for(int j=0;j<300;j++)
            {
                if(i==j)cost[i][j]=0;
                else cost[i][j]=inf;
            }
        double a,b;
        while(~scanf("%lf%lf",&a,&b)){
                int m=n;
                while(1){
                    if(a==-1&&b==-1)break;
                    e[n++]={a,b};
                    scanf("%lf%lf",&a,&b);
                }
            for(int i=m+1;i<n;i++)
            {
                cost[i][i-1]=cost[i-1][i]=dis(e[i],e[i-1])*3.0/2000;
            }
        }
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                cost[i][j]=min(cost[i][j],dis(e[i],e[j])*6.0/1000);
        dijkstra();
        printf("%.0f ",d[1]);
        return 0;
    }
  • 相关阅读:
    C++中使用多线程
    hdu 4223 dp 求连续子序列的和的绝对值最小值
    hdu 1372 bfs 计算起点到终点的距离
    hdu 4217 线段树 依次取第几个最小值,求其sum
    心得
    hdu 1175 bfs 按要求进行搜索,是否能到达,抵消两个(相同)棋子
    hdu 4221 greed 注意范围 工作延期,使整个工作时间罚时最少的单个罚时最长的值
    hdu 2844 多重背包 多种硬币,每一种硬币有一点数量,看他能组成多少种钱
    uva LCDDisplay
    hdu 4218 模拟 根据一个圆点和半径画一个圆 注意半径要求
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/6480467.html
Copyright © 2011-2022 走看看