HDU 1245 Saving James Bond

來源:互聯網
上載者:User

標籤:最短路   spfa   計算幾何   

計算幾何+SPFA


我已經不想看我的提交記錄了。。。。

HDU 我起碼WA了2頁。。。。

都是浮點數惹的禍。

const double eps=1e-4;abs(a-b)<=eps;

這樣來判斷相等。


總共 n 條鱷魚,最多有 n*(n+1)/2 條路。

抽象化處理。

把 中心的起點當作 起點0 ; 最多有 n+1 條路。

把鱷魚和周圍的邊界的終點都當作 n+1 ; 最多有 n+1 條

總共就只存在 n+2個點。


就是計算0 和 n+1 的最短距離。


有個小最佳化,就是當 跳躍距離能夠直接跳到岸上的時候就直接輸出 42.5 1;


G++ ,C++ 都過了。


#include<cstdio>#include<cstring>#include<string>#include<queue>#include<cmath>#include<algorithm>#include<queue>#include<map>#include<iostream>using namespace std;const double INF= 0xfffffff;bool vis[103];double dis[103];int path[103];int n;double m;const double eps=1e-4;struct lx{    int v;    double len;};vector<lx>g[103];struct node{    double x,y;} point[101];void build(){    int u,v;    for(int i=1; i<=n; i++)    {        double x=point[i].x;        double y=point[i].y;        double len;        u=i;        double xx,yy;        lx now;        xx=min(50.00-x,50.00-y);        yy=min(50.00+x,50.00+y);        len=min(xx,yy);        if(len<=m)        {            now.v=n+1,now.len=len;            g[u].push_back(now);        }        len=sqrt(x*x+y*y);        if(len-7.5<=m)        {            now.len=len-7.5;            now.v=u;            g[0].push_back(now);        }        for(int j=i+1; j<=n; j++)        {            len=sqrt((x-point[j].x)*(x-point[j].x)+(y-point[j].y)*(y-point[j].y));            if(len<=m)            {                now.len=len;                now.v=j;                g[u].push_back(now);                now.v=u;                g[j].push_back(now);            }        }    }}void SPFA(){    for(int i=0; i<103; i++)        dis[i]=INF,vis[i]=0,path[i]=INF;    queue<int>q;    dis[0]=0,vis[0]=1;    q.push(0);path[0]=0;    while(!q.empty())    {        int u=q.front();        q.pop();        vis[u]=0;        for(int j=0; j<g[u].size(); j++)        {            int v=g[u][j].v;            double len=g[u][j].len;            if(abs(dis[v]-dis[u]-len)<=eps)            {                if(path[v]>path[u]+1)                path[v]=path[u]+1;            }            else if(dis[v]>=dis[u]+len)            {                dis[v]=dis[u]+len;                path[v]=path[u]+1;                if(!vis[v])                {                    vis[v]=1;                    q.push(v);                }            }        }    }    if(abs(dis[n+1]-INF)<=eps)        puts("can't be saved");    else        printf("%.2f %d\n",dis[n+1],path[n+1]);}int main(){    while(cin>>n>>m)    {        for(int i=0; i<103; i++)            g[i].clear();        for(int i=1; i<=n; i++)            scanf("%lf%lf",&point[i].x,&point[i].y);        if(m>=42.50)        {            puts("42.50 1");            continue;        }        build();        SPFA();    }}



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.