zoukankan      html  css  js  c++  java
  • Subway POJ

    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

    此题复杂之处是建图方式

    其实对于此题来讲建立邻接表其实与建立邻接矩阵的是等效的

    而邻接矩阵还需要vector 会出现复杂的stl容器之间的嵌套

    所以此题采用邻接表+朴素的dijk

    之前的bellmanford的tle代码

    在此题之中 最坏的情况是200的三次方 +vector 存图 自然会tle

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<map>
    #include<math.h>
    #include<vector>
    #include<queue>
    using namespace std;
    typedef pair<double, double> P;
    P s,e;
    map<P,double> dis;//到达任意点的距离
    vector<pair<P,P> > mp;
    double dist1(P q, P p)
    {
        return sqrt((q.first-p.first)*(q.first-p.first)+(q.second-p.second)*(q.second-p.second))/10.0;
    }
    double dist2(P q, P p)
    {
        return sqrt((q.first-p.first)*(q.first-p.first)+(q.second-p.second)*(q.second-p.second))/40.0;
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        P tmp;
        P lst;
        lst.first=-1;
        scanf("%lf%lf%lf%lf",&s.first,&s.second,&e.first,&e.second);
        dis[s]=dist1(s,s);
        dis[e]=dist1(e,s);
        while(~scanf("%lf%lf",&tmp.first,&tmp.second))
        {
            if(tmp.first==-1&&tmp.second==-1)
            {
                continue;
            }
            else
            {
                dis[tmp]=dist1(tmp,s);
                if(lst.first!=-1)
                {
                    mp.push_back(make_pair(tmp,lst));
                    mp.push_back(make_pair(lst,tmp));
                }
            }
            lst=tmp;
        }
        for(int i=1; i<=dis.size(); i++)
        {
            for(vector<pair<P,P> >::iterator it=mp.begin(); it!=mp.end(); it++)
            {
                //cout<< it->first.first <<" "<< it->first.second <<"->"<<it->second.first <<" "<< it->second.second <<"dis"<<dist2(it->first,it->second)<<endl;
                if(dis[it->first]+dist2(it->first,it->second)<dis[it->second])
                {
                    //cout<<"*"<<endl;
                    dis[it->second]=dis[it->first]+dist2(it->first,it->second);
                }
            }
            for(map<P,double>::iterator it1=dis.begin(); it1!=dis.end(); it1++)
            {
                for(map<P,double>::iterator it2=dis.begin(); it2!=dis.end(); it2++)
                {
                    if(it1->second+dist1(it1->first,it2->first)<it2->second)
                    {
                        it2->second=it1->second+dist1(it1->first,it2->first);
                    }
                }
            }
        }
    //    for(int i=1; i<=dis.size(); i++)
    //    {
    //        for(map<P,double>::iterator it1=dis.begin(); it1!=dis.end(); it1++)
    //        {
    //            for(map<P,double>::iterator it2=dis.begin(); it2!=dis.end(); it2++)
    //            {
    //                if(it1->second+dist1(it1->first,it2->first)<it2->second)
    //                {
    //                    it2->second=it1->second+dist1(it1->first,it2->first);
    //                }
    //            }
    //        }
    //    }
        printf("%d
    ",int(dis[e]*60/1000+0.5));
        return 0;
    }

    附上修改之后的正解 邻接矩阵存图+dijk

    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <cstdlib>
    #include <ctime>
    #include <string>
    #include <map>
    #include <set>
    #include <queue>
    #include <stack>
    #include <algorithm>
    #include <iostream>
    using namespace std;
    typedef pair<double,double > P; //邻接表存图 建立映射
    P a[1000]; //记录节点对应位置
    double mp[1000][1000];//建立邻接矩阵存图
    int vis[1000];
    double dis[1000];
    P s,e;
    int cnt=0;
    double dist1(int q,int p)
    {
        return sqrt((a[q].first-a[p].first)*(a[q].first-a[p].first)+(a[q].second-a[p].second)*(a[q].second-a[p].second))/10.0;
    }
    double dist2(int q,int p)
    {
        return sqrt((a[q].first-a[p].first)*(a[q].first-a[p].first)+(a[q].second-a[p].second)*(a[q].second-a[p].second))/40.0;
    }
    double dij()
    {
        vis[1]=1;
        dis[1]=0;
        int  MIN,k;
        for(int i=1; i<=cnt; i++) dis[i]=mp[1][i];
        for(int i=1; i<cnt; i++)
        {
            MIN=1e9;
            k=-1;
            for(int j=1; j<=cnt; j++)
            {
                if(dis[j]<MIN&&vis[j]==0)
                {
                    MIN=dis[j];
                    k=j;
                }
            }
            if(k==-1) break;
            vis[k]=1;
            for(int j=1; j<=cnt; j++)
            {
                if(dis[k]+mp[k][j]<dis[j])
                {
                    dis[j]=dis[k]+mp[k][j];
                }
            }
        }
        return dis[2];
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        P tmp;
        P lst;
        memset(vis,0,sizeof(vis));
        memset(mp,0,sizeof(mp));
        memset(dis,0x3f,sizeof(dis));
        scanf("%lf%lf%lf%lf",&s.first,&s.second,&e.first,&e.second);
        cnt++;
        a[cnt]=s;
        cnt++;
        a[cnt]=e;
        mp[2][1]=dist1(1,2);
        lst.first=-1;
        while(~scanf("%lf%lf",&tmp.first,&tmp.second))
        {
            if(tmp.first!=-1)
            {
                cnt++;
                a[cnt]=tmp;
                for(int i=1; i<=cnt; i++)
                {
                    mp[cnt][i]=dist1(cnt,i);
                }
                if(lst.first!=-1) mp[cnt][cnt-1]=dist2(cnt,cnt-1);
            }
            lst=tmp;
        }
        for(int i=1; i<=cnt; i++)
        {
            for(int j=1; j<=cnt; j++)
            {
                if(mp[i][j]==0) mp[i][j]=mp[j][i];
            }
        }
        printf("%d
    ",int(dij()*60/1000+0.5));
        return 0;
    }
    
  • 相关阅读:
    python编写规范
    我们分析了400位华语歌手的歌词,发现人们重点关注的人事物情
    外部厂商公开工具
    OSI七层与TCP/IP五层网络架构详解
    npm run dev--The 'mode' option has not been set, webpack will fallback to 'production' for this value
    webpack-dev-server --inline --progress --config build/webpack.dev.conf.js
    PPTV(pplive)_forap_1084_9993.exe 木马清除经历
    【转】【Nginx】Nginx 入门教程 + 常用配置解析
    【转】【Python】Python 中文编码报错
    【Centos】systemd入门教程
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852306.html
Copyright © 2011-2022 走看看