Poj2502-spfa

传送门-----------------------------------subway

Subway
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11732 Accepted: 3835

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

思路:

spfa模板题练手。

需要注意的有几点:第一点是同一条地铁线上只有相邻的两站才可以加边如1.2、2.3

第二点是最后结果的四舍五入

第三点式步行加边的时候不能把地铁加边的边覆盖

CODE:

#include<iostream>
#include<math.h>
#include<cstdio>
#include<cstring>
#include<queue> 
using namespace std;
const double INF=1<<30;
const double v1=40000.0/60,v2=10000.0/60;  
int cnt;
bool vis[205];
double d[205],g[205][205];
struct point{
	int x,y;
}p[210];
 double dis(point a,point b)
 {
 	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
 }

void spfa()
{
	int f,i;
	queue<int>q;
	for(i = 0;i<=cnt;i++) d[i]=INF,vis[i]=0;
	q.push(1);
	vis[1]=1;
	d[1]=0;
	while(!q.empty())
	{
		f=q.front();
		q.pop();
		vis[f]=0;
		for(i =1;i<cnt;i++)
		{
			if(g[f][i]>0&&d[i]>d[f]+g[f][i])
			{
				d[i]=d[f]+g[f][i];
				if(!vis[i])
				  vis[i]=1,q.push(i);
			}
		}
	}
}
int main()
{
	int i,j,x,y,last;
	for(i = 1;i<=205;i++)
	for(j = 1;j<=205;j++)
	g[i][j]=0;
	scanf("%d%d%d%d",&p[1].x,&p[1].y,&p[2].x,&p[2].y);
	last=cnt=3;
    while(~scanf("%d%d",&x,&y))
    {
    	if(x==-1&&y==-1)
    	{
    		last=cnt;
    		continue;
		}
		else
		{
		p[cnt].x=x;
		p[cnt].y=y;
		if(cnt!=last)	
		g[cnt][cnt-1]=g[cnt-1][cnt]=dis(p[cnt],p[cnt-1])/v1;
		cnt++;
		}
	}
for(i = 1;i<cnt;i++)
for(j = 1;j<cnt;j++)
if(g[i][j]==0)g[i][j]=g[j][i]=dis(p[i],p[j])/v2;

spfa();
printf("%.d\n",int(d[2]+0.5));
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值