By the Underground or by Foot?

http://vjudge.net/contest/view.action?cid=46683#problem/B


#include <iostream>
#include "math.h"
#include <list>
#include <iomanip>
#define INF 0x7f7f7f7f
using namespace std;

struct coordinate
{
	double x,y;//必须用double,float答案精确度会错 
}coo[205];

double dist[205][205];//两坐标之间时间距离 
int c[205][205];//存储前驱点的矩阵 
list <int> ::iterator itr;

void FSP(list<int> &l,int b,int e)//Find the Shortest Path
{
	if(c[b][e] != 0)
	{
		itr = l.begin();
		for( ; *itr != b;itr ++ ) ;//之前空循环忘记分号,导致下一句被认为是for内容,程序异常 
		//cout<<*itr<<endl;
		itr++;
		l.insert(itr,c[b][e]);
		//for(itr=l.begin() ; itr != l.end();itr ++ )cout<<*itr<<" ";
		FSP(l,b,c[b][e]);
		FSP(l,c[b][e],e);
	}
	else ;
}

int main()
{
	for(int i = 0 ; i < 205 ; i ++)
		for(int j = 0 ; j < 205 ; j ++)
		{
			dist[i][j] = INF;//初始化各点距离为无穷 
		    c[i][j] = 0;//初始化前驱矩阵 
		}
	double vfoot,vunder;
	cin >> vfoot >> vunder;
	int n;
	cin >> n;
	list <int> lans;
	
	for(int i = 1; i <= n ; i ++)
	    cin >> coo[i].x >> coo[i].y;//按序号输入车站坐标 

	int a,b;
	while(1)
	{
		cin >> a >> b;
		if( a == 0 && b == 0) break;
	    else dist[a][b] = dist[b][a] = sqrt( pow( (coo[a].x-coo[b].x) ,2 )+pow( (coo[a].y-coo[b].y) ,2 ) )/vunder;//有连线,时间距离为距离除以地铁速度 
	}
	cin >> coo[n+1].x >> coo[n+1].y;//起点
	cin >> coo[n+2].x >> coo[n+2].y;//终点 
	
	for(int i = 1 ; i <= n+2 ; i ++)
	    for(int j = 1 ; j <= n+2 ; j ++)
	        if(dist[i][j] == INF )//地铁不通
			     dist[i][j]=dist[j][i]=sqrt( pow( (coo[i].x-coo[j].x) ,2 )+pow( (coo[i].y-coo[j].y) ,2 ) )/vfoot;//无连线,时间距离为距离除以步行速度 
	
	//此时由n+2个点形成的联通图已经做出,开始计算最短路径和经过的点 
	for(int k = 1; k <=n+2;k++ )
	    for(int i = 1; i <=n+2;i++ )
	        for(int j = 1; j <=n+2;j++ )
	        {
				if(dist[i][j] > dist[i][k] + dist[k][j])
				{
					c[i][j]=k;
					dist[i][j] = dist[i][k] + dist[k][j];
				}
			}
	lans.push_back(n+1);//起点 
	lans.push_back(n+2);//终点 
	FSP(lans,lans.front(),lans.back());
	
	cout<<fixed<<setprecision(6)<<dist[n+1][n+2]<<endl;
	cout<<lans.size()-2<<" ";//因为我的程序把首尾节点也加进了队列 
	while(!lans.empty())
	{
		a = lans.front();
		lans.pop_front();
		if(a != n+1 && a != n+2)cout<<a<<" ";
	}
	/*测试时间距离矩阵 
	for(int i = 1 ; i <= n+2 ; i ++)
	{
		for(int j = 1 ; j <= n+2 ; j ++)
	    {
			cout<<dist[i][j]<<" ";
		}
		cout<<endl;
	}
	*/
	//记得小数位数 
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值