[bzoj2709][Violet 1]迷宫花园(二分+spfa)

题目:

我是超链接

题解:

二分+spfa,话说这个题目给的样例。。。。中间那么多空行怪不得全给我爆零,连的最大边数要考虑清楚

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#define N 40050
using namespace std;
const double eps=1e-7;
int n,m,s,t;bool vis[N],chan[N];
int tot,nxt[N],point[N],v[N];double cc[N],dis[N],L;
char st[105][105];
void addline(int x,int y,double z,int fl)
{
    ++tot; nxt[tot]=point[x]; point[x]=tot; v[tot]=y; cc[tot]=z; chan[tot]=fl;
    ++tot; nxt[tot]=point[y]; point[y]=tot; v[tot]=x; cc[tot]=z; chan[tot]=fl;
}
bool spfa(double mid)
{
    queue<int>q;
    for (int i=1;i<=tot;i++) if (chan[i]) cc[i]=mid;
    memset(vis,0,sizeof(vis));
    memset(dis,0x7f,sizeof(dis));
    dis[s]=0;
    q.push(s);
    while (!q.empty())
    {
        int now=q.front(); q.pop(); vis[now]=0;
        for (int i=point[now];i;i=nxt[i])
            if (dis[v[i]]>dis[now]+cc[i])
            {
                dis[v[i]]=dis[now]+cc[i];
                if (!vis[v[i]]) vis[v[i]]=1,q.push(v[i]);
            }
    }
    if (dis[t]<=L) return 1;
    return 0;
}
int main()
{
    int T,i,j;
    scanf("%d",&T);
    while (T--)
    {
        tot=0; memset(point,0,sizeof(point));memset(chan,0,sizeof(chan));
        scanf("%lf%d%d\n",&L,&n,&m);
        for (i=1;i<=n;i++)
        {
            gets(st[i]+1);
            for (j=1;j<=m;j++)
              if (st[i][j]=='S') s=(i-1)*m+j;
              else if (st[i][j]=='E') t=(i-1)*m+j;
        }
        for (i=1;i<=n;i++)
          for (j=1;j<=m;j++)
            if (st[i][j]==' '||st[i][j]=='E'||st[i][j]=='S')
            {
                int now=(i-1)*m+j;
                if (j+1<=m && (st[i][j+1]==' '||st[i][j+1]=='E'||st[i][j+1]=='S')) 
                  addline(now,now+1,1,0);
                if (i+1<=n && (st[i+1][j]==' '||st[i+1][j]=='E'||st[i+1][j]=='S')) 
                  addline(now,now+m,0,1);
            }
        double l=0,r=10;
        while (r-l>=eps)
        {
            double mid=(l+r)/2.0;
            if (spfa(mid)) l=mid;
            else r=mid;
        }
        printf("%.5lf\n",l);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值