二分v(上下移动花费)的值,跑最短路验证
code:
#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<complex>
#include<iostream>
#include<algorithm>
#define ll long long
#define inf 1e7
using namespace std;
inline void read(char &c)
{
while(!((c=getchar())==32||c=='#'||c=='S'||c=='E'));
}
const int maxn = 11000;
const int maxm = 41000;
const int maxr = 105;
const int maxc = 105;
const double eps = 1e-7;
const int dx[]={-1,0,1,0};
const int dy[]={0,1,0,-1};
int idn,n,m,st,ed;
double L;
struct edge
{
int y,nex;
double c;
}a[maxm]; int len,fir[maxn];
inline void ins(const int x,const int y,const double c)
{
a[++len].y=y; a[len].c=c;
a[len].nex=fir[x]; fir[x]=len;
}
struct node
{
int i;
double x;
node(){}
node(const double _x,const int _i){x=_x;i=_i;}
};
inline bool operator <(node x,node y){return x.x>y.x;}
double d[maxn];
priority_queue<node>q;
double search()
{
for(int i=0;i<=idn;i++) d[i]=inf;
d[st]=0.0; q.push(node(0,st));
while(!q.empty())
{
const node x=q.top(); q.pop();
if(fabs(d[x.i]-x.x)>eps) continue;
for(int k=fir[x.i];k;k=a[k].nex)
{
const int y=a[k].y;
if(d[y]>x.x+a[k].c)
{
d[y]=x.x+a[k].c;
q.push(node(d[y],y));
}
}
}
return d[ed];
}
int id[maxr][maxc];
void build_(const double c)
{
len=0; for(int i=1;i<=idn;i++) fir[i]=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++) if(id[i][j])
{
const int idx=id[i][j];
for(int k=0;k<4;k++)
{
int x=i+dx[k],y=j+dy[k];
if(id[x][y]) ins(idx,id[x][y],(k==0||k==2)?c:1.0);
}
}
}
}
double solve()
{
double l=0.0,r=10;
while(r-l>eps)
{
double mid=(l+r)/2;
build_(mid);
double tmp=search();
if(tmp<=L) l=mid+eps;
else r=mid-eps;
}
if(l==0.0) l=eps+eps;
return l-eps;
}
char str;
char sss[10];
int main()
{
int t; scanf("%d",&t);
while(t--)
{
memset(id,0,sizeof id);
scanf("%lf%d%d",&L,&n,&m); idn=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
read(str);
if(str=='#') continue;
id[i][j]=++idn;
if(str==32) continue;
if(str=='S') st=idn;
if(str=='E') ed=idn;
}
gets(sss);
}
printf("%.5lf\n",solve());
}
return 0;
}