标签:
input | output |
---|---|
1 100 4 0 0 1 0 9 0 9 9 1 2 1 3 2 4 0 0 10 10 10 0 |
2.6346295 4 4 2 1 3 |
#include <iostream> #include <cstring> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <time.h> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #define inf 0x3f3f3f3f #define mod 10000 typedef long long ll; using namespace std; const int N=205; const int M=100005; int n,m,k=0; double a,b; int vis[N],pre[N]; double w[N][N],d[N]; char str[20]; bool flag=false; struct man{ double x,y; }point[N]; void spfa() { d[0]=0; vis[0]=1; queue<int>q; q.push(0); while(!q.empty()){ int t=q.front();q.pop(); vis[t]=0; for(int i=0;i<=n+1;i++){ if(d[i]>d[t]+w[t][i]){ d[i]=d[t]+w[t][i]; pre[i]=t; if(!vis[i])q.push(i),vis[i]=1; } } } } int main() { for(int i=0;i<N;i++){ d[i]=10000000; for(int j=0;j<N;j++){ w[i][j]=10000000; } } scanf("%lf%lf",&a,&b); scanf("%d",&n); int u,v; for(int i=1;i<=n;i++){ scanf("%lf%lf",&point[i].x,&point[i].y); } while(~scanf("%d%d",&u,&v)&&u&&v){ double s=sqrt((point[u].x-point[v].x)*(point[u].x-point[v].x)+(point[u].y-point[v].y)*(point[u].y-point[v].y)); w[u][v]=w[v][u]=s/b; } scanf("%lf%lf%lf%lf",&point[0].x,&point[0].y,&point[n+1].x,&point[n+1].y); for(int i=0;i<=n+1;i++){ for(int j=i+1;j<=n+1;j++){ if(w[i][j]<10000000)continue; double s=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)); w[i][j]=w[i][j]=s/a; } } spfa(); printf("%.7lf\n",d[n+1]); stack<int>p; for(int i=n+1;pre[i];i=pre[i]){ p.push(pre[i]); } printf("%d",p.size()); while(!p.empty())printf(" %d",p.top()),p.pop(); return 0; }
URAL 1205 By the Underground or by Foot?(SPFA)
标签:
原文地址:http://www.cnblogs.com/jianrenfang/p/5875457.html