二分+最短路 uvalive 3270 Simplified GSM Network(推荐)

时间:2023-12-26 15:09:19
 // 二分+最短路 uvalive 3270 Simplified GSM Network(推荐)
// 题意:已知B(1≤B≤50)个信号站和C(1≤C≤50)座城市的坐标,坐标的绝对值不大于1000,每个城市使用最近的信号站。给定R(1≤R≤250)条连接城市线路的描述和Q(1≤Q≤10)个查询,求相应两城市间通信时最少需要转换信号站的次数。
// 思路:建议先阅读 NOI论文 <<计算几何中的二分思想>>
// 直接献上题解吧:
// 二分!
// l的两端点所属信号站相同:w[l]=0。
// 否则若|l|<e(蓝):w[l]=1。
// 否则,将线段l沿中点(红)分开:
// l=l1+l2,w[l]=w[l1]+w[l2]。
// 对l1与l2进行同样操作。
// 详细请看论文 #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
using namespace std;
#define LL long long
typedef pair<int,int> pii;
const int inf = 0x3f3f3f3f;
const int MOD = ;
const int N = ;
const int maxx = ;
#define clc(a,b) memset(a,b,sizeof(a))
const double eps = 1e-;
void fre() {freopen("in.txt","r",stdin);}
void freout() {freopen("out.txt","w",stdout);}
inline int read() {int x=,f=;char ch=getchar();while(ch>''||ch<'') {if(ch=='-') f=-; ch=getchar();}while(ch>=''&&ch<='') {x=x*+ch-'';ch=getchar();}return x*f;}
struct Point{
double x,y;
Point(){}
Point(double _x,double _y){
x = _x;y = _y;
}
Point operator -(const Point &b)const{
return Point(x - b.x,y - b.y);
}
double operator ^(const Point &b)const{
return x*b.y - y*b.x;
}
double operator *(const Point &b)const{
return x*b.x + y*b.y;
}
}b[],c[];
int B,C,n,q; struct node{
int v,c;
node(int vv,int cc){
v=vv;
c=cc;
}
node(){}
bool operator <(const node &r) const{
return c>r.c;
}
}; struct edge{
int v,cost;
edge(int vv=,int ccost =):v(vv),cost(ccost){}
}; vector<edge>e[N];
bool vis[N];
int dist[N];
void dij(int start){
clc(vis,false);
for(int i=;i<=n+;i++) dist[i]=inf;
priority_queue<node>q;
while(!q.empty()) q.pop();
dist[start]=;
q.push(node(start,));
node tmp;
while(!q.empty()){
tmp=q.top();
q.pop();
int u=tmp.v;
if(vis[u]) continue;
vis[u]=true;
for(int i=;i<e[u].size();i++){
int v=e[u][i].v;
int cost=e[u][i].cost;
if(!vis[v]&&dist[v]>dist[u]+cost){
dist[v]=dist[u]+cost;
q.push(node(v,dist[v]));
}
}
}
}
void add(int u,int v,int w){
e[u].push_back(edge(v,w));
} double dis(Point a,Point b){
return sqrt((a-b)*(a-b));
} int belong(Point a){
double len=(double)inf;
int num;
for(int i=;i<=B;i++){
if(dis(a,b[i])<len){
len=dis(a,b[i]),num=i;
}
}
return num;
} Point getmid(Point a,Point b){
return Point((a.x+b.x)/,(a.y+b.y)/);
} int calc(Point a,Point b){
if(belong(a)==belong(b)) return ;
if(dis(a,b)<1e-) return ;
else return calc(a,getmid(a,b))+calc(getmid(a,b),b);
} int main(){
// fre();
int cas=;
while(~scanf("%d%d%d%d",&B,&C,&n,&q),B,C,n,q){
for(int i=;i<=;i++) e[i].clear();
for(int i=;i<=B;i++){
scanf("%lf%lf",&b[i].x,&b[i].y);
}
for(int i=;i<=C;i++){
scanf("%lf%lf",&c[i].x,&c[i].y);
}
for(int i=;i<=n;i++){
int u,v;
scanf("%d%d",&u,&v);
add(u,v,calc(c[u],c[v]));
add(v,u,calc(c[v],c[u]));
}
printf("Case %d:\n",cas++);
while(q--){
int u,v;
scanf("%d%d",&u,&v);
dij(u);
if(dist[v]>=inf) printf("Impossible\n");
else printf("%d\n",dist[v]);
}
}
return ;
}