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; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步