平面上有 n ( n ≤ 200 ) n(n\le 200) n(n≤200) 个矩形,矩形的边平行于坐标轴,现在度度熊需要操控一名角色从 A A A 点走到 B B B 点。
该角色可以上下左右移动,在恰被 k k k 个矩形覆盖的区域,该角色的速率为 k + 1 k+1 k+1 个距离/秒(矩形覆盖区域包括边界)。
请求出 A A A 移动到 B B B 最快需要多少秒。
不同的
x
,
y
x,y
x,y把整张图划分成一个网格图,那么我们只要在网格图内跑最短路即可.
判断一条边被覆盖了多少次,直接
f
o
r
n
for~n
for n个矩阵看是否覆盖即可.
总复杂度:
O
(
n
3
+
n
2
log
n
)
O(n^3+n^2\log n)
O(n3+n2logn).
#include<bits/stdc++.h>
#define gc getchar()
#define SZ(a) ((int)a.size())
#define all(a) a.begin(),a.end()
#define mk make_pair
#define pi pair<int,int>
#define fi first
#define se second
#define ll long long
#define ull unsigned ll
#define ld long double
#define vi vector<int>
#define pb push_back
#define TP template<class o>
#define IT iterator
using namespace std;
const int N=410,mod=998244353,inf=2e9;
TP void qr(o &x) {
char c=gc; x=0; int f=1;
while(!isdigit(c)) {if(c=='-') f=-1; c=gc;}
while(isdigit(c))x=x*10+c-'0',c=gc;
x*=f;
}
TP void qw(o x) {
if(x<0) putchar('-'),x=-x;
if(x/10) qw(x/10);
putchar(x%10+'0');
}
TP void pr1(o x) {qw(x); putchar(' ');}
TP void pr2(o x) {qw(x); puts("");}
struct P {
int x,y;
bool operator <=(P b) const {return x<=b.x&&y<=b.y;}
bool operator >=(P b) const {return x>=b.x&&y>=b.y;}
} s,t;
struct rec {P a,b;} p[N];
int n,id[N][N],num;
struct edge{int y,next;double d;} a[N*N*4];int len,last[N*N];
void ins(int x,int y,double z) {a[++len]=(edge){y,last[x],z}; last[x]=len;}
void add(int x,int y,double z) {ins(x,y,z); ins(y,x,z);}
double dijkstra(int st,int ed) {
static double d[N*N];
priority_queue<pair<double,int> > q;
double tmp=1e18;
for(int i=0;i<=num;i++) d[i]=tmp;
d[st]=0; q.push(mk(0,st));
while(q.size()) {
double D=-q.top().fi;
int x=q.top().se; q.pop();
if(x==ed) return D;
if(D!=d[x]) continue;
for(int k=last[x],y;k;k=a[k].next)
if(d[y=a[k].y]>d[x]+a[k].d) {
d[y]=d[x]+a[k].d;
q.push(mk(-d[y],y));
}
}
}
int V(P x,P y) {
int cnt=1;
for(int i=1;i<=n;i++)
cnt += (p[i].a<=x&&y<=p[i].b);
return cnt;
}
int main() {
int T; qr(T); while(T--) {
qr(n); vi X,Y;
for(int i=1;i<=n;i++) {
qr(p[i].a.x);
qr(p[i].a.y);
qr(p[i].b.x);
qr(p[i].b.y);
X.pb(p[i].a.x);
X.pb(p[i].b.x);
Y.pb(p[i].a.y);
Y.pb(p[i].b.y);
}
qr(s.x); qr(s.y); qr(t.x); qr(t.y);
X.pb(s.x); X.pb(t.x);
Y.pb(s.y); Y.pb(t.y);
sort(all(X)); X.erase(unique(all(X)),X.end());
sort(all(Y)); Y.erase(unique(all(Y)),Y.end());
num=0; len=0; memset(last,0,sizeof last);
for(int i=0;i<SZ(X);i++)
for(int j=0;j<SZ(Y);j++) {
id[i][j]=num++;
if(i) add(id[i-1][j],id[i][j],1.0*(X[i]-X[i-1])/V((P){X[i-1],Y[j]},(P){X[i],Y[j]}));
if(j) add(id[i][j-1],id[i][j],1.0*(Y[j]-Y[j-1])/V((P){X[i],Y[j-1]},(P){X[i],Y[j]}));
}
s.x=lower_bound(all(X),s.x)-X.begin();
s.y=lower_bound(all(Y),s.y)-Y.begin();
t.x=lower_bound(all(X),t.x)-X.begin();
t.y=lower_bound(all(Y),t.y)-Y.begin();
printf("%.5lf\n",dijkstra(id[s.x][s.y],id[t.x][t.y]));
}
return 0;
}