POJ 2502-Subway(最短路水题)

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

题目大意:给出起点和终点的坐标,给出多条线,每行给出线上的点坐标,以-1-1为该线路的结束符。在线上的速度为40km/h,不在线上速度为10km/h,坐标之间的单位为m,求起点到终点的最短路。

思路:可恨啊,我居然被这水题困了几天.......(但这个好像有必要思考一下)

题意明确求最短路,但是,怎么抽象出这个图呢,全抽吗?每条边都算出来吗?不对吧,光这样就n^2了呀,线段上可取的点超多耶,那应该 就是在取边上进行选择优化,怎么搞呢.....

事实却是:题目限制不超过200个站点.......mmp(还一度去网上找建图的总结)。就tm是个水题。

代码如下:

#include<set>
#include<map>
#include<list>
#include<deque>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<stdio.h>
#include<sstream>
#include<stdlib.h>
#include<string.h>
//#include<ext/rope>
#include<iostream>
#include<algorithm>
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
#define per(i,a,b) for(int i=a;i<=b;++i)
#define LL long long 
#define swap(a,b) {int t=a;a=b;b=t} 
using namespace std;
//using namespace __gnu_cxx;
struct node{
       double x;
       double y;
}p[205];
int vis[205];
double d[205];
double mp[205][205];
double fun(node a,node b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{
    int u,n;
    double s,t;
    int fag=0;
    scanf("%lf%lf%lf%lf",&p[0].x,&p[0].y,&p[1].x,&p[1].y);   //起始位置和目的地坐标
    n=2;
    memset(mp,0,sizeof(mp));
    while(scanf("%lf%lf",&p[n].x,&p[n].y)!=EOF)
    {
       if(p[n].x==-1&&p[n].y==-1)
        {
            fag=0;
            continue;
        }
        if(fag!=0)
        {
            t=fun(p[n],p[n-1])/40000.0;//相邻兩點距离 
            mp[n][n-1]=mp[n-1][n]=t;
        }
        n++;
        fag=1;
    }
    per(i,0,n-1)                                                                                              //计算需要步行的所用的时间
    {
	   per(j,0,n-1)
       { 
	       if(i!=j&&mp[i][j]==0.0)
           {
		       mp[i][j]=mp[j][i]=fun(p[j],p[i])/10000.0;//所有点间的距离 
		   }
	   }
	} 
    memset(vis,0,sizeof(vis));   //Dijkstra算法求解最短路
    vis[0]=1;
    per(i,0,n-1) d[i]=mp[0][i];
    per(i,1,n-1)
    {
        t=INF;
        per(j,0,n-1)
        {
		   if(!vis[j]&&d[j]<t)
           {
               t=d[j];
               u=j;
           }
        }
        vis[u]=1;
        per(j,0,n-1)
        {
		    if(!vis[j])
            {
                t=d[u]+mp[u][j];
                if(t<d[j])
                    d[j]=t;
            }
        }    
    }
    s=60.0*d[1];          //题目所要求的单位是分钟
    printf("%0.0lf\n",s);
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值