4 10 17 0 27 0 37 0 45 0 1 10 20 30
42.50 5 can't be saved
二级最短路算法:
方法一
#include"stdio.h" #include"string.h" #include"iostream" #include"map" #include"string" #include"queue" #include"stdlib.h" #include"math.h" #define M 40 #define eps 1e-10 #define inf 99999999 #define mod 1000000000 using namespace std; struct st { int u,v,next; double w; }edge[30000]; int head[111],use[111],n,t,time[111]; double dis[111]; struct node { double x,y; }p[111]; double pow(double x) { return x*x; } double Len(node a,node b) { return sqrt(pow(a.x-b.x)+pow(a.y-b.y)); } void init() { t=0; memset(head,-1,sizeof(head)); } void add(int u,int v,double w) { edge[t].u=u; edge[t].v=v; edge[t].w=w; edge[t].next=head[u]; head[u]=t++; } void bfs(int S) { int i; queue<int>q; memset(use,0,sizeof(use)); for(i=0;i<=n;i++) { time[i]=dis[i]=inf; } dis[S]=time[S]=0; q.push(S); while(!q.empty()) { int u=q.front(); q.pop(); use[u]=1; for(i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(dis[v]>dis[u]+edge[i].w) { dis[v]=dis[u]+edge[i].w; time[v]=time[u]+1; } if(fabs(dis[v]-dis[u]-edge[i].w)<eps) { if(time[v]>time[u]+1) time[v]=time[u]+1; } if(!use[v]) q.push(v); } } } int judge(double x,double y) { if(x>=-50&&x<=50&&y>=-50&&y<=50&&x*x+y*y>=7.5*7.5) return 1; return 0; } int main() { int m,i,j; double d; while(scanf("%d%lf",&m,&d)!=-1) { p[0].x=p[0].y=0; for(i=1;i<=m;i++) scanf("%lf%lf",&p[i].x,&p[i].y); init(); for(i=1;i<=m;i++) { for(j=i+1;j<=m;j++) { if(judge(p[i].x,p[i].y)&&judge(p[j].x,p[j].y)) { double L=Len(p[i],p[j]); if(L<=d) { add(i,j,L); add(j,i,L); } } } } n=m+1; for(i=1;i<=m;i++) { if(judge(p[i].x,p[i].y)) { double L=Len(p[0],p[i]); if(L-7.5<=d) { add(0,i,L-7.5); add(i,0,L-7.5); } double L1=min(50-p[i].x,50+p[i].x); double L2=min(50-p[i].y,50+p[i].y); L=min(L1,L2); if(L<=d) { add(i,n,L); add(n,i,L); } } } if(d>=50-7.5) { add(0,n,50-7.5); add(n,0,50-7.5); } bfs(0); if(dis[n]<inf) printf("%.2lf %d\n",dis[n],time[n]); else printf("can't be saved\n"); } return 0; }
#include"stdio.h" #include"string.h" #include"math.h" #define M 111 #define inf 99999999 #define eps 1e-8 #include"iostream" using namespace std; struct node { double x,y; }p[M]; int use[M],time[M][M],mint[M],n; double dis[M],G[M][M]; double pow(double x) { return x*x; } double Len(node a,node b) { return sqrt(pow(a.x-b.x)+pow(a.y-b.y)); } void dij(int s) { int i; memset(use,0,sizeof(use)); for(i=0;i<=n;i++) { dis[i]=G[s][i]; mint[i]=time[s][i]; } mint[s]=0; dis[s]=0; use[s]=1; int w=n; while(w--) { double min=inf; int tep=-1; for(i=0;i<=n;i++) { if(!use[i]&&dis[i]<min) { min=dis[i]; tep=i; } } if(tep==-1) return; use[tep]=1; for(i=0;i<=n;i++) { if(!use[i]&&dis[i]>dis[tep]+G[tep][i]) { dis[i]=dis[tep]+G[tep][i]; mint[i]=mint[tep]+time[tep][i]; } else if(!use[i]&&fabs(dis[i]-dis[tep]-G[tep][i])<eps) { if(mint[i]>mint[tep]+time[tep][i]) mint[i]=mint[tep]+time[tep][i]; } } } } int ok(double x,double y) { if(x>=-50&&y>=-50&&x<=50&&y<=50&&x*x+y*y>=7.5*7.5) return 1; return 0; } int main() { int m,i,j; double d; while(scanf("%d%lf",&m,&d)!=-1) { n=m+1; for(i=0;i<=n;i++) { for(j=0;j<=n;j++) time[i][j]=G[i][j]=inf; G[i][i]=time[i][i]=0; } p[0].x=p[0].y=0; for(i=1;i<=m;i++) { scanf("%lf%lf",&p[i].x,&p[i].y); } for(i=1;i<=m;i++) { for(j=1;j<=m;j++) { if(ok(p[i].x,p[i].y)&&ok(p[j].x,p[j].y)) { double L=Len(p[i],p[j]); if(d>=L) { G[i][j]=G[j][i]=L; time[i][j]=time[j][i]=1; } } } } for(i=1;i<=m;i++) { if(ok(p[i].x,p[i].y)) { double L1=min(50-p[i].x,50+p[i].x); double L2=min(50-p[i].y,50+p[i].y); double L=min(L1,L2); if(d>=L) { G[i][n]=G[n][i]=L; time[i][n]=time[n][i]=1; } L=Len(p[i],p[0])-7.5; if(d>=L) { G[0][i]=G[i][0]=L; time[0][i]=time[i][0]=1; } } } if(d>=42.5) { G[0][n]=G[n][0]=42.5; time[0][n]=time[n][0]=1; } dij(0); if(dis[n]<inf) printf("%.2lf %d\n",dis[n],mint[n]); else printf("can't be saved\n"); } return 0; }