• hdu 1245 Saving James Bond 策画几何+最短路 最短路求步数最少的路径


    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #define inf 0x3fffffff
    #define N 200
    #define eps  1e-10
    #include<queue>
    using namespace std;
    struct node {
     int x,y;
    }ma[N];
    struct nodee {
    int u,v,next;
    double w;
    }bian[N*N];
    int yong,head[N],n;
    void addedge(int u,int v,double w) {
    bian[yong].u=u;
    bian[yong].v=v;
    bian[yong].w=w;
    bian[yong].next=head[u];
    head[u]=yong++;
    }
    double MIN(double  a,double b) {
    return a>b?b:a;
    }
    void  bfs() {//宽搜
        queue<int>q;
        double dist[N];
        int step[N],i,visit[N];
        memset(visit,0,sizeof(visit));
        while(!q.empty())
            q.pop();
        q.push(0);
        for(i=1;i<=n+1;i++) 
            dist[i]=inf;
        step[0]=0;
       dist[0]=0;visit[0]=1;
        while(!q.empty()){
            int cur=q.front();
            q.pop();
            for(i=head[cur];i!=-1;i=bian[i].next) {
                int v=bian[i].v;
                if((dist[v]>dist[cur]+bian[i].w+eps||fabs(dist[v]-dist[cur]-bian[i].w)<=eps)&&step[v]>step[cur]+1&&visit[v]==0) {//判断条件如果相等就要比较它的最少的步数
                    step[v]=step[cur]+1;
                    dist[v]=dist[cur]+bian[i].w;
                    visit[v]=1;
                    q.push(v);
                }
            }
        }
        if(dist[n+1]<inf)
            printf("%.2f %d
    ",dist[n+1],step[n+1]);
        else
            printf("can't be saved
    ");
            return ;
    }
    int main() {
         int i,j,s,t;
         double limit,minn;
         while(scanf("%d%lf",&n,&limit)!=EOF) {
                t=n+1;s=0;
                yong=0;
                memset(head,-1,sizeof(head));
            for(i=1;i<=n;i++) {
                scanf("%d%d",&ma[i].x,&ma[i].y);
              minn=MIN(fabs(50.0-fabs(1.0*ma[i].x)),fabs(50.0-fabs(1.0*ma[i].y)));
                if(minn<limit+eps) {//将符合条件的边
                    addedge(i,t,minn);
                    addedge(t,i,0);//
                }
                minn=fabs(sqrt(1.0*ma[i].x*ma[i].x+1.0*ma[i].y*ma[i].y)-7.5);
                 if(minn<limit+eps) {
                  addedge(s,i,minn);//
                  addedge(i,s,0);//
                 }
         }
        if(limit+eps>42.50) {//如果可以直接跳到岸上就直接输出
                    printf("42.50 1
    ");
                    continue;
                }
          for(i=1;i<n;i++)
          for(j=i+1;j<=n;j++) {
            minn=inf;
            minn=MIN(minn,sqrt(1.0*(ma[i].x-ma[j].x)*(ma[i].x-ma[j].x)+1.0*(ma[i].y-ma[j].y)*(ma[i].y-ma[j].y)));
            if(minn<limit+eps) {
                addedge(i,j,minn);
                addedge(j,i,minn);//双向边
            }
          }
          bfs();
         }
    return 0;
    }
    


  • 相关阅读:
    C# List<T>用法(转)
    任务列表 (Visual Studio)
    TSQL行转列、列转行
    HRESULT:0x80070057 (E_INVALIDARG)的异常的解决方案(转)
    JS正则表达式详解(转)
    Windows远程登录命令
    JavaScript中的try...catch和异常处理(转)
    javascript如何触发button 的click事件
    在ASP.NET中利JavaScript实现控件的聚焦(转)
    输入一个字符串,将其逆序后输出。(使用C++,不建议用伪码)
  • 原文地址:https://www.cnblogs.com/thefirstfeeling/p/4410710.html
Copyright © 2020-2023  润新知