您的位置:首页 > 其它

【建模】【最短路】

2015-08-24 11:45 302 查看
Subway

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 7252Accepted: 2362
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

#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;
    
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define mp push_back

const int maxn = 310;
struct Node
{
	double x,y;
}node[maxn];

double g[maxn][maxn];
double d[maxn];
bool ok[maxn];
int n;

double dis(int a,int b)
{
	return sqrt((node[a].x-node[b].x)*(node[a].x-node[b].x) + (node[a].y - node[b].y)*(node[a].y-node[b].y));
}

double dij()
{
	for(int i=1;i<=n;i++)
	{
		d[i] = 0x3f3f3f3f;
		ok[i] = false;
	}
	d[1] = 0;
	for(int i=0;i<n-1;i++)
	{
		int mins = 0x3f3f3f3f;
		int p = -1;
		for(int j=1;j<=n;j++)
		{
			if(!ok[j] && d[j] < mins)
			{
				mins = d[j];
				p = j;
			}
		}
		if(p == -1) break;
		ok[p] = true;
		for(int j=1;j<=n;j++)
		{
			if(!ok[j] && d[j] > d[p] + g[j][p])
			{
				d[j] = d[p] + g[j][p];
			}
		}
	}
	return d[2];
}
int main()
{
	double v1 = 10000.0/60;
	double v2 = 40000.0/60;
   scanf("%lf%lf%lf%lf",&node[1].x,&node[1].y,&node[2].x,&node[2].y); 
	{
		int tx,ty;
		n = 2;
		bool beg = true;
		for(int i=0;i<maxn;i++)
		{
			for(int j=0;j<maxn;j++)
			{
				if(i == j) g[i][j] = 0;
				else 
				g[i][j] = 0x3f3f3f3f;
			}
		}
		
		while(scanf("%d%d",&tx,&ty)!=EOF)
		{
			if(tx == -1 && ty == -1) 
			{
				beg = true;
				continue;
			}
		
			n ++;
			node
.x = tx;
			node
.y = ty;
			if(!beg) 
			{
			g[n-1]
 = g
[n-1] = min(g
[n-1],dis(n,n-1)/v2);

			}
			beg = false;
		}

		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=n;j++)
			{
				g[i][j]  = min(g[i][j],dis(i,j)/v1);
			}
		}
		printf("%d\n",int(0.5+dij()));
	}
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: