• 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;
    }
  • 相关阅读:
    练习选择菜单(optionmenu)、上下文菜单(Contextmenu)、弹出菜单(popupmenu)综合小demo
    Androidstudio中listView视图列表控件的使用小练习
    MintUI的MessageBox的用法
    Hbuilder打包app后相机拍摄失效问题的解决
    Vue自带Eslint规范经常报的错误信息
    Object.defineProperty属性实现双向绑定
    移动端开发注意事项
    浏览器兼容问题
    http请求详解
    web页面性能优化及seo
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/6480467.html
Copyright © 2020-2023  润新知