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;
}