poj——2502——Subway

来源:互联网 发布:淘宝网旧版首页 编辑:程序博客网 时间:2024/05/22 15:07

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 10000 200 5000 200 7000 200 -1 -1 2000 600 5000 600 10000 600 -1 -1

Sample Output

21

乘坐地铁从家到学校,地铁40km/h  步行10km/h , 已知各个站点的x,y坐标,输入的信息每个列次用-1,-1隔开,要求花费的时间最少
分析:把家和学校各看成一个站点,求出相邻站点的权值(路程/速度),以站点数量建立map,保留两站之间的距离,然后dijsktra算法寻找两站点间最短路径

#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <cstdio>#include <iomanip>using namespace std;#define inf 10000000.0const int M=210;double  map[M][M],dist[M],path[M][M];int vis[M];double dijkstra(int x,int y,int n){  double Min;  vis[x]=1;  for(int i=0;i<n;i++)  {      if(!vis[i])      dist[i]=path[0][i];  }  for(int i=1;i<n;i++)  {      Min=inf;      int temp;      for(int j=0;j<n;j++)      {          if(!vis[j]&&dist[j]<Min)          {              Min=dist[j];              temp=j;          }      }      vis[temp]=1;      if(temp==y)      break;      for(int j=0;j<n;j++)      {          if(!vis[j]&&dist[j]>dist[temp]+path[temp][j])          {              dist[j]=dist[temp]+path[temp][j];          }      }  }  return dist[y];}int main(){    int num=2,flag=0;    memset(vis,0,sizeof(vis));    memset(path,0,sizeof(path));    cin>>map[0][0]>>map[0][1]>>map[1][0]>>map[1][1];    while(cin>>map[num][0]>>map[num][1])    {        if(map[num][0]==-1&&map[num][1]==-1)        {            flag=0;            continue;        }        if(flag)        {           path[num][num-1]=path[num-1][num]=(sqrt(pow(map[num][0]-map[num-1][0],2.0)+pow(map[num][1]-map[num-1][1],2.0)))/40000.0;        }        num++;        flag=1;    }    for(int i=0;i<num;i++)    {        for(int j=0;j<num;j++)        {            if(path[i][j]==0)            {               path[i][j]=path[j][i]=(sqrt(pow(map[i][0]-map[j][0],2.0)+pow(map[i][1]-map[j][1],2.0)))/10000.0;            }        }    }    printf("%0.lf\n",dijkstra(0,1,num)*60);    return 0;}


0 0