poj2502Subway(SPFA)
阿新 • • 發佈:2019-01-05
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<queue> #include<cmath> #define INF 0x3f3f3f3f using namespace std; const int maxn=200+10; double dis[maxn],gra[maxn][maxn]; bool vis[maxn]; int cal; struct Point { int x,y; }point[maxn]; queue<int>Q; double caldistance(Point a,Point b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } void Spfa() { while(!Q.empty()) Q.pop(); for(int i=1;i<=cal;i++) { dis[i]=INF*1.0; vis[i]=false; } dis[1]=0; vis[1]=true; Q.push(1); while (!Q.empty()) { int temp=Q.front(); Q.pop(); vis[temp]=false; for(int i=1;i<=cal;i++) { if (dis[temp]+gra[temp][i]<dis[i]) { dis[i]=dis[temp]+gra[temp][i]; if(!vis[i]) { Q.push(i); vis[i]=true; } } } } } int main() { int u,v; int last_point; double v1=40000.0/60; double v2=10000.0/60; while(~scanf("%d%d%d%d",&point[1].x,&point[1].y,&point[2].x,&point[2].y)) { cal=2,last_point=-1; memset(gra,0,sizeof(gra)); while(scanf("%d%d",&u,&v)==2) { if(u==-1&&v==-1) { last_point=-1; continue; } if(last_point==-1) { point[++cal].x=u, point[cal].y=v; last_point=cal; } else { point[++cal].x=u; point[cal].y=v; gra[last_point][cal]=gra[cal][last_point]=caldistance(point[last_point],point[cal])/v1; last_point=cal; } } for(int i=1;i<cal;i++) for(int j=i+1;j<=cal;j++) if(gra[i][j]==0) gra[i][j]=gra[j][i]=caldistance(point[i],point[j])/v2; Spfa(); printf("%d\n",int(dis[2]+0.5));//四捨五入 } return 0; }