【最短路】POJ2502 SUBWAY (spfa)
阿新 • • 發佈:2018-12-22
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
Sample Output
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.
0 0 10000 1000 0 200 5000 200 7000 200 -1 -1 2000 600 5000 600 10000 600 -1 -1
21
題意:給你起點和終點,下面每一行是一條地鐵線,每兩個相鄰整數代表一個地鐵站的座標;地鐵站之間只能相鄰站點行駛;地鐵40km/h,步行10km/h;題中的座標是以米為單位的;
思路:從起點1到終點k的最短路;都清楚題意就可以了,我的錯誤在於沒有注意地鐵站之間只能相鄰站點行駛的條件;
程式碼:
以上兩種四捨五入為整數的輸出方式均可,注意第二種輸出方式只等用%f!#include<cstdio> #include<cstdlib> #include<cmath> #include<algorithm> #include<cstring> #include<iostream> #include<map> #include<queue> using namespace std; const int maxn=205; const double inf=0x3f3f3f3f; int k,l; struct node { double x,y; int num; } s[maxn]; bool vis[maxn]; double dis[maxn]; void spfa(int st) { memset(vis,false,sizeof(vis)); for(int i=0;i<maxn;i++) dis[i]=inf; vis[st]=true; dis[st]=0; queue<int > que; while(!que.empty()) que.pop(); que.push(st); while(!que.empty()) { int u=que.front(); vis[u]=false; que.pop(); for(int i=1; i<=k; i++) { if(i==u) continue; double far,t; far=sqrt(((s[u].y-s[i].y)*(s[u].y-s[i].y))+((s[u].x-s[i].x)*(s[u].x-s[i].x))); far=far/1000.0; if(fabs(s[u].num-s[i].num)==1) t=far/40.0; else t=far/10.0; if(dis[i]>dis[u]+t) { dis[i]=dis[u]+t; if(!vis[i]) { vis[i]=true; que.push(i); } } } } } int main() { double s1,s2,e1,e2; double x,y; k=2,l=1; scanf("%lf%lf%lf%lf",&s1,&s2,&e1,&e2); while(~scanf("%lf%lf",&x,&y)) { if(x==-1&&y==-1) { if(l==8) break; l++; continue; } s[k].x=x,s[k].y=y; s[k++].num=l++; } s[1].x=s1,s[1].y=s2,s[1].num=-1; s[k].x=e1,s[k].y=e2,s[k].num=-3; spfa(1); printf("%d\n",(int)(dis[k]*60+0.5)); // printf("%.0f\n",floor(dis[k]*60+0.5)); return 0; }