您的位置:首页 > 其它

poj2502 - Subway (dijkstra )(L)

2017-07-10 17:50 441 查看
转载自:http://blog.csdn.net/wangjian8006

subway



Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 10129Accepted: 3307
Description
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

题目大意:首先给你两个坐标,一个是你家里的坐标,一个是你学校的坐标,然后接下来有若干条地铁线,每条地铁线上有若干个站点,给出每个站点的坐标,有这些点,这些点当中有距离,这个距离的单位是米,现在告诉你走路是10km/h,做地铁的话是40km/h,问你从家里到学校所花费的最短时间(分钟)

解题思路:这个主要是卡的输入,只要将输入弄好了,构成一张图就好做了,然后需要注意的是,对于地铁线,你从1站到2站的速度是40km/h,但是不代表你从1站到3站的速度是40km/h,你必须经过2站才能到3站,然后我们构图的时候权值保存的是时间多少分钟(可能会有从a到b点有多个分钟,取最小的),这里单位换算也要注意下,然后就是直接模版了

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define inf 0x3f3f3f3f
#define maxn 300
#define mine(a,b) (a<b?a:b)

struct node
{
int x,y;
}point[maxn],a;
double distant(node a,node b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double value[maxn][maxn];
int n;
int ex(node t)
{
int i;
for(i=1;i<n;i++)
{
if(point[i].x==t.x&&point[i].y==t.y)break;
}
if(i==n)point[n++]=t;//如果不存在该点 则将该点加入point中
return i;
}
double dis[maxn];
bool vis[maxn];
void dijkstra()
{
memset(vis,false,sizeof vis);
for(int i=2;i<n;i++)
dis[i]=value[1][i];
dis[1]=0;
for(int i=1;i<n;i++)
{
int pos=-1;
for(int j=1;j<n;j++)
{
if(!vis[j]&&(pos==-1||dis[j]<dis[pos]))
pos=j;
}
vis[pos]=true;
for(int j=1;j<n;j++)
{
if(!vis[j]&&dis[j]>dis[pos]+value[pos][j])
{
dis[j]=dis[pos]+value[pos][j];
}
}
}
}
int main()
{
int subway[maxn],cnt;
for(int i=1;i<maxn;i++)
for(int j=1;j<maxn;j++)
if(i==j)value[i][j]=0;
else value[i][j]=inf;
n=1;
scanf("%d%d%d%d",&point
.x,&point
.y,&point[n+1].x,&point[n+1].y);
n+=2;
//int t=2;//用于测试
//while(t--)
{
while(~scanf("%d%d",&a.x,&a.y))
//scanf("%d%d",&a.x,&a.y);
{
cnt=0;
subway[cnt++]=ex(a);;//用于判断输入的点是否存在 即这个点在point中的下标为多少
while(~scanf("%d%d",&a.x,&a.y))
{
if(a.x==-1&&a.y==-1)break;
subway[cnt++]=ex(a);
}
for(int i=1;i<cnt;i++)//这条铁路线上各个站点之间的时间
{
value[subway[i]][subway[i-1]]=value[subway[i-1]][subway[i]]=
distant(point[subway[i-1]],point[subway[i]])*3/2000;
}
}
}

for(int i=1;i<n;i++)
for(int j=1;j<i;j++)
{
value[i][j]=value[j][i]=min(value[j][i],distant(point[i],point[j])*3/500);
}
dijkstra();
printf("%d\n",(int)(dis[2]+0.5));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: