1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> #define fo(i,a,b) for(int i=a;i<=b;i++) using namespace std;
typedef long long LL;
const int maxn=255, maxp=63000, maxe=18e6+5; const double sml=1e-7;
struct P{ int r,c; }; bool cmp(const P &a,const P &b) {return a.r<b.r || a.r==b.r && a.c>b.c;}
int n,m,w,sum,x[maxn],y[maxn]; P p1[maxn],p[maxn];
int tot,go[maxe],val[maxe],next[maxe],f1[maxp]; void ins(int x,int y,int z) { go[++tot]=y; val[tot]=z; next[tot]=f1[x]; f1[x]=tot; }
double DIS(int a,int b) { return sqrt((double)(x[a]-x[b])*(x[a]-x[b])+(double)(y[a]-y[b])*(y[a]-y[b])); }
LL dis[maxp],inf; int d[8*maxp]; bool bz[maxp]; void spfa() { memset(dis,127,sizeof(dis)); dis[0]=0; inf=dis[1]; bz[0]=1; d[1]=0; int maxi=7*maxp; for(int i=1, j=1, ii=1, jj=1; ii<=jj; ii++, i=i%maxi+1) { for(int p=f1[d[i]]; p; p=next[p]) if (dis[go[p]]>dis[d[i]]+val[p]) { dis[go[p]]=dis[d[i]]+val[p]; if (!bz[go[p]]) { jj++; j=j%maxi+1; bz[ d[j]=go[p] ]=1; if (dis[d[i%maxi+1]]>dis[d[j]]) swap(d[i%maxi+1],d[j]); } } bz[d[i]]=0; } }
int T; int main() { scanf("%d",&T); while (T--) { scanf("%d %d %d",&n,&m,&w); sum=n*m+1; fo(i,1,n) scanf("%d %d",&x[i],&y[i]); fo(i,1,m) scanf("%d %d",&p1[i].r,&p1[i].c); sort(p1+1,p1+1+m,cmp); int m0=0; fo(i,1,m) { p[++m0]=p1[i]; while (m0>1 && p[m0-1].r<=p[m0].r && p[m0-1].c>=p[m0].c) { m0--; p[m0]=p[m0+1]; } } m=m0; tot=0; memset(f1,0,sizeof(f1)); fo(i,1,n) fo(j,1,n) if (i!=j) { double ds=DIS(i,j)-sml; int jm=m+1; fo(im,1,m) { while (jm>1 && ds<=(LL)p[im].r+p[jm-1].r) jm--; if (jm && jm<=m) ins((i-1)*m+im,(j-1)*m+jm,p[jm].c); } } fo(i,1,n) fo(im,1,m) { if (p[im].r>=y[i]) ins(0,(i-1)*m+im,p[im].c); if (p[im].r>=w-y[i]) ins((i-1)*m+im,sum,0); if (im<m) ins((i-1)*m+im,(i-1)*m+im+1,p[im+1].c-p[im].c); } spfa(); if (dis[sum]==inf) printf("impossible\n"); else printf("%lld\n",dis[sum]); } }
|