[BZOJ2709] [Violet 1]迷宫花园

http://www.lydsy.com/JudgeOnline/problem.php?id=2709
这题是权限题,版权问题不放题意,我来简要的说一下:
给出一个迷宫,有起始点终点,可以上下左右走,移动的耗时是1,可以改变上下走的耗时为v,给出一个总耗时L,问在总耗时中的v是多少


显然这题可以先二分答案v然后跑宽搜或者spfa来判断,还是蛮简单的。
这题的难点就在于,看到v的范围很小( 0v10 )就没想到要二分,但事实上,这题的精度要求很大,所以要判断的v范围还是挺大的。
好在图不是很大,所以可以多次跑spfa等算法算最短路。


code:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int dx[]={-1,0,1,0};
const int dy[]={0,-1,0,1};
const double eps=1e-7;
const double INF=1e7;
int ids[110][110],id;
int R,C;
double L;
int st,ed;
char ss[110][110];
struct node
{
    int x,y,next;
    double c;
}a[210000]; int len,last[110000];
void ins(int x,int y,double c)
{
    len++;
    a[len].x=x;a[len].y=y;a[len].c=c;
    a[len].next=last[x];last[x]=len;
}
double d[11000];
int list[110000],head,tail;
bool v[110000];
double spfa()
{
    for(int i=1;i<=id;i++) d[i]=INF;
    memset(v,false,sizeof(v)); v[st]=true;
    d[st]=0.0; head=1,tail=2; list[head]=st;
    while(head!=tail)
    {
        int x=list[head];
        for(int k=last[x];k;k=a[k].next)
        {
            int y=a[k].y;
            if(d[y]>d[x]+a[k].c)
            {
                d[y]=d[x]+a[k].c;
                if(v[y]==false)
                {
                    //v[y]=true;
                    list[tail++]=y;
                }
            }
        }
        head++;v[x]=false;
    }
    return d[ed];
}
void build(double c)
{
    len=0; memset(last,0,sizeof(last));
    for(int i=1;i<=R;i++)
    {
        for(int j=1;j<=C;j++)
        {
            int x=ids[i][j];
            for(int k=0;k<4;k++)
            {
                int tx=i+dx[k],ty=j+dy[k];
                if(tx>=1 && tx<=R && ty>=1 && ty<=C && ss[tx][ty]!='#')
                {
                    int y=ids[tx][ty];
                    ins(x,y,(k==0||k==2)?c:1.0);
                }
            }
        }
    }
}
int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        memset(ids,0,sizeof(ids));
        scanf("%lf%d%d\n",&L,&R,&C);
        id=0;
        for(int i=1;i<=R;i++)
        {
            gets(ss[i]+1);
        }
        for(int i=1;i<=R;i++)
        {
            for(int j=1;j<=C;j++)
            {
                if(ss[i][j]=='#') continue;
                ids[i][j]=++id;
                if(ss[i][j]==' ') continue;
                else if(ss[i][j]=='S') st=id;
                else if(ss[i][j]=='E') ed=id; 
            }
        }
        double l=0.0,r=10.0;
        while(r-l>eps)
        {
            double mid=(l+r)/2;
            build(mid);
            double tt=spfa();
            if(tt<=L) l=mid+eps;
            else r=mid-eps;
        }
        printf("%.5lf\n",l);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值