POJ2502 Subway【最短路+技巧】

Enjoy_process發表於2018-10-20

Subway

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14147   Accepted: 4584

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

Sample Output

21

Source

Waterloo local 2001.09.22

問題連結:POJ2502 Subway

問題描述:從家出發可以通過步行(10 km/h)和地鐵(40 km/h)兩種方式到學校,用二維座標(x,y)描述家,學校,地鐵站,描述地鐵線的方式是通過順序給出這條線上的地鐵站位置,以(-1,-1)表示此次地鐵線的結束,其中x和y的單位是m。同一條地鐵線的相鄰兩個站是直線(雙向),並且你到達地鐵站後可以立即乘地鐵,地鐵站的總個數不超過200,問從家到學校的最短時間是多少分鐘(四捨五入)

解題思路:將所有點都獨立出來,包括家,學校和地鐵站,由於點的總數不操作202,因此使用鄰接矩陣表示圖,需要解決的問題之一就是如何構建圖g:g[i][j]表示結點i到結點j的時間(單位:h),非地鐵站到其他點以及地鐵站到非同一條線的地體站只能通過步行,因此只要用歐幾里得距離除以10000m/h就能計算得出,對於同一條地鐵線上的站,由於只有在相鄰的站之間是直線,才能通過歐幾里得距離處理40000m/h計算時間,不相鄰的只能暫時通過步行方法計算得出時間,如何在程式中計算這些時間呢?這有一個小技巧,在點的結構體中有一個id屬性,如果兩個點的id值相差為1,那麼它們就是同一條地鐵線且相鄰的站,否則就不是。程式學校編號為0,學校編號為1,構建好圖後,就使用最短路演算法計算最短路即可,程式使用spfa演算法,最後的到的結果是小時,化成分鐘後四捨五入即可:int(dist[1]*60+0.5)

AC的C++程式碼:

#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>

using namespace std;

const int N=300;
const double INF=9000000.0;
const double SP1=40000.0;
const double SP2=10000.0;

double g[N][N];
struct Node{
	double x,y;
	int id;
}p[N];

//返回結點個數 
int read()
{
	double x,y;
	int k=0,id=3;
	while(~scanf("%lf%lf",&x,&y)){
		id++;
		if(x==-1&&y==-1) continue;
		p[k].x=x,p[k].y=y,p[k].id=id;
		k++;
	}
	p[0].id=0,p[1].id=2;
	for(int i=0;i<k;i++)
	  for(int j=i;j<k;j++)
	    if(i==j)
	      g[i][j]=0;
	    else if(p[i].id-p[j].id==-1)//如果在同一條地鐵線上 
	      g[i][j]=g[j][i]=sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y))/SP1;
	    else
		  g[i][j]=g[j][i]=sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y))/SP2; 
	return k;
}
//起點編號為0 終點編號為1 求起點到終點的最短路徑
double dist[N];
bool vis[N];
int cnt[N];

bool spfa(int n,int s)//結點數和起點 
{
	memset(dist,INF,sizeof(dist));
	memset(vis,false,sizeof(vis));
	memset(cnt,0,sizeof(cnt));
	cnt[s]=1;
	vis[s]=true;
	dist[s]=0;
	queue<int>q;
	q.push(s);
	while(!q.empty()){
		int f=q.front();
		q.pop();
		vis[f]=false;
		for(int i=0;i<n;i++)
		  if(dist[i]>dist[f]+g[f][i]){
		  	dist[i]=dist[f]+g[f][i];
		  	if(!vis[i]){
		  		q.push(i);
		  		cnt[i]++;
		  		vis[i]=true;
		  		if(cnt[i]>n)
		  		  return false;
			  }
		  }
	}
	return true;
}


int main()
{
	int n=read();
	if(spfa(n,0))
	  printf("%d\n",int(dist[1]*60+0.5));
	return 0;
} 

 

相關文章