POJ2502 Subway(建图+Dijkstra)

题目链接

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

思路

这道题的解法本身是Dijkstra的板子,但建图方面比较麻烦。根据题意我们首先需要记录起点与终点,然后对地铁的相邻站之间建边,最后构建其余点之间的连接,求出起点到终点的最短路即可(题目中实际为最短时间)。

代码部分

#include<map>
#include<stack>
#include<queue>
#include<string>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int P=139;
const int mod=149;
const int maxn=1e5+5;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int minn=0xc0c0c0c0;
struct node
{
    
    
	int x,y;
}a[maxn];
int m,n;
bool vis[1000];
double dis[1000],maze[1000][1000];
void dij()
{
    
    
	for(int i=0;i<n;i++)
		dis[i]=inf;
	memset(vis,false,sizeof vis);
	dis[0]=0;
	for(int i=0;i<n;i++)
	{
    
    
		int u=-1,Min=inf;
		for(int j=0;j<n;j++)
		{
    
    
			if(!vis[j]&&dis[j]<Min)
			{
    
    
				u=j;
				Min=dis[j];
			}
		}
		vis[u]=true;
		for(int j=0;j<n;j++)
		{
    
    
			if(!vis[j]&&dis[j]>dis[u]+maze[u][j])
				dis[j]=dis[u]+maze[u][j];//x
		}
	}
	printf("%.0lf\n",dis[1]);
}
int main()
{
    
    
    //ios::sync_with_stdio(false);
	//cin.tie(0);cout.tie(0);
	while(cin>>a[0].x>>a[0].y>>a[1].x>>a[1].y)
	{
    
    
		for(int i=0;i<1000;i++)
		{
    
    
			for(int j=0;j<1000;j++)
			{
    
    
				maze[i][j]=inf;//初始化慎用memset
			}
		}
		n=m=2;
		while(cin>>a[n].x>>a[n].y)
		{
    
    
			if(a[n].x==-1&&a[n].y==-1)
			{
    
    
				m=n;
				continue;
			}
			if(n!=m)
			{
    
    
				double d=sqrt((double)(a[n].x-a[n-1].x)*(a[n].x-a[n-1].x)+(a[n].y-a[n-1].y)*(a[n].y-a[n-1].y));
				d=3*d/2000;
				maze[n][n-1]=maze[n-1][n]=d;
			}
			n++;
		}
		for(int i=0;i<=n;i++)
		{
    
    
			for(int j=0;j<=n;j++)
			{
    
    
				if(maze[i][j]==inf)
				{
    
    
					double d=sqrt((double)(a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y));
					d=3*d/500;
					maze[i][j]=maze[j][i]=d;
				}
			}
		}
		dij();
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/WTMDNM_/article/details/108528268
今日推荐