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.
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
题意:给你一个起点坐标,一个终点坐标,几条地铁线路,同一线路在同一直线上,你只能在相邻的地铁站之间乘坐地铁,其余走路。给出走路速度与地铁速度,问最短多少时间能到达目的地。
题解:首先题目给的是m/h,先换成m/min。然后将起点定为点1,终点为点2,按照一条条地铁线路输入,每两座相邻的地铁站为一条无向边,找最短路。另外,先将距离初始化为INF,再输入地铁边以及地铁时间,再将其他边赋成步行时间。
也就是说,我们可以先构造完这个图,再用最短路。
代码:
#include<algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cassert> #include <cstdio> #include <vector> #include <string> #include <cmath> #include <queue> #include <stack> #include <set> #include <map> using namespace std; #define P(a,b,c) make_pair(a,make_pair(b,c)) #define rep(i,a,n) for (int i=a;i<=n;i++) #define per(i,a,n) for (int i=n;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef pair<int,pair<int,int> >pii; typedef long long ll; const ll mod = 1000000007; ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; } const int MAXN=300; struct Node { double x,y; }node[MAXN]; double dis(Node a,Node b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } const double INF=1e30; bool vis[MAXN]; double dist[MAXN]; double cost[MAXN][MAXN]; void Dijkstra(int n,int start) { for(int i=1;i<=n;i++) { dist[i]=INF; vis[i]=false; } dist[start]=0; for(int j=0;j<n;j++) { int k=-1; double Min=INF; for(int i=1;i<=n;i++) if(!vis[i]&&dist[i]<Min) { Min=dist[i]; k=i; } vis[k]=true; for(int i=1;i<=n;i++) if(!vis[i]&&dist[k]+cost[k][i]<dist[i]) dist[i]=dist[k]+cost[k][i]; } } int main() { double v1=10000.0/60; double v2=40000.0/60; while(scanf("%lf%lf%lf%lf",&node[1].x,&node[1].y,&node[2].x,&node[2].y)==4) { int n=2; int cnt1=3; int x,y; for(int i=1;i<300;i++) for(int j=1;j<300;j++) { if(i==j)cost[i][j]=0; else cost[i][j]=INF; } while(scanf("%d%d",&x,&y)==2) { if(x==-1&&y==-1) { cnt1=n+1; continue; } n++; node[n].x=x; node[n].y=y; if(n!=cnt1)cost[n][n-1]=cost[n-1][n]=dis(node[n],node[n-1])/v2; } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cost[i][j]=min(cost[i][j],dis(node[i],node[j])/v1); Dijkstra(n,1); printf("%d\n",(int)(dist[2]+0.5)); } return 0; }