http://acm.timus.ru/problem.aspx?space=1&num=1205
spfa
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<vector> #include<map> #include<cmath> #include<queue> #include<stack> #include<algorithm> using namespace std; const int INF=0x3f3f3f3f; const int N=205; double cost[N][N]; struct node { double x,y; }point[N]; int f[N]; double Fdist(int i,int j) { return sqrt((point[i].x-point[j].x)*(point[i].x-point[j].x)+ (point[i].y-point[j].y)*(point[i].y-point[j].y)); } double spfa(int s,int m,int &I) { bool in[N]; double dist[N]; for(int i=0;i<=m;++i) {in[i]=false;dist[i]=-1.0;} queue<int>str; str.push(s); in[s]=true; dist[0]=0.0; while(!str.empty()) { int x=str.front();str.pop();//cout<<x<<" "<<dist[x]<<endl; in[x]=false; for(int i=0;i<=m;++i) { if(i!=x&&(dist[i]<0.0||dist[i]>dist[x]+cost[x][i])) { dist[i]=dist[x]+cost[x][i]; f[i]=x; if(!in[i]) { in[i]=true; str.push(i); } } } } int k=f[m]; I=0; while(k!=0) { ++I; k=f[k]; } return dist[m]; } void dfs(int x) { if(f[x]!=0) dfs(f[x]); printf(" %d",x); } int main() { //freopen("data.txt","r",stdin); double fv,uv; int n; while(scanf("%lf %lf",&fv,&uv)!=EOF) { scanf("%d",&n); for(int i=0;i<=n+1;i++) cost[i][i]=0.0; for(int i=1;i<=n;++i) { scanf("%lf %lf",&point[i].x,&point[i].y); for(int j=1;j<i;++j) { cost[i][j]=cost[j][i]=Fdist(i,j)/fv; } } int l1,l2; while(scanf("%d %d",&l1,&l2)) { if(l1==0&&l2==0) break; cost[l1][l2]=cost[l2][l1]=min(cost[l1][l2],Fdist(l1,l2)/uv); } scanf("%lf %lf",&point[0].x,&point[0].y); scanf("%lf %lf",&point[n+1].x,&point[n+1].y); for(int i=0;i<=n+1;++i) {cost[i][0]=cost[0][i]=Fdist(0,i)/fv;cost[n+1][i]=cost[i][n+1]=Fdist(n+1,i)/fv;} int I; printf("%.6lf\n",spfa(0,n+1,I)); printf("%d",I); if(I) dfs(f[n+1]); printf("\n"); } return 0; }