Subway(最短路)
阿新 • • 發佈:2018-12-22
Subway
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 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 is the number of minutes it will take you to get to school, rounded to the nearest minute, taking the fastest route.
Time Limit: 1000MS | Memory Limit: 65536K |
Total Submissions: 11248 | Accepted: 3672 |
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
Output
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
稠密圖額最短路,關鍵在於輸入和建邊;邊的權值為耗費的時間
同一地鐵線路中相鄰兩點建立;任意兩點建立步行權值的邊;
注意輸出浮點數的四捨五入;資料量較小,dijkstra()floyd()均可~
#include<iostream> #include<algorithm> #include<queue> #include<vector> #include<cstring> #include<utility> #include<cmath> #include<cstdio> using namespace std; const double v1=10000.0/60.0; const double v2=40000.0/60.0; const int maxn=333; double cost[maxn][maxn]; struct node{int x,y;}; node a[maxn]; int sz; const int inf=0x3f3f3f3f; void init() { for(int i=1;i<=300;i++) { for(int j=1;j<=300;j++) i==j?cost[i][j]=0:cost[i][j]=inf; } } double get(int aa,int bb) { return sqrt((double)(a[aa].x-a[bb].x)*(a[aa].x-a[bb].x)+(a[aa].y-a[bb].y)*(a[aa].y-a[bb].y)); } double dis[maxn]; int book[maxn]; typedef pair<double,int>pr; void dijkstra() { for(int i=1;i<=sz;i++)dis[i]=inf; memset(book,0,sizeof(book));dis[1]=0; priority_queue<pr,vector<pr>,greater<pr> >q; q.push(make_pair(0,1)); while(!q.empty()) { pr tt=q.top();q.pop(); double d=tt.first;int x=tt.second; if(book[x])continue;book[x]=1; for(int i=1;i<=sz;i++) { if(dis[i]>dis[x]+cost[x][i]) { dis[i]=dis[x]+cost[x][i]; q.push(make_pair(dis[i],i)); } } } } int main() { init(); cin>>a[1].x>>a[1].y>>a[2].x>>a[2].y; sz=2;int cnt1=3; int x,y; while(scanf("%d %d",&x,&y)==2) { if(x==-1&&y==-1){ cnt1=sz+1; continue; } ++sz;a[sz].x=x;a[sz].y=y; if(sz!=cnt1)cost[sz][sz-1]=cost[sz-1][sz]=(min(cost[sz][sz-1],get(sz,sz-1))/v2); } for(int i=1;i<=sz;i++) { for(int j=1;j<=sz;j++) { cost[j][i]=cost[i][j]=min(cost[i][j],get(i,j)/v1); } } dijkstra(); printf("%0.0lf\n",dis[2]); return 0; }