Subway POJ - 2502 (最短路)

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\n",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\n",int(dij()*60/1000+0.5));
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值