HDU-4643-GSM(DFS)
base station when moving on the train. Xiao Ming would like to know how many times the base station will change from city A to city B.
Now, the problem is simplified. We assume the route of train is straight, and the mobile phone will receive the signal from the nearest base station.
0<=y<=1000, both x and y is integer).Then there is a number : K, the next, there are K queries, for each query, each line, there are two numbers: a, b.
4 4
0 2
1 3
1 0
2 0
1 2
1 1
2 2
2 1
4
1 2
1 3
1 4
3 4
0
1
2
1HintThe train way from a to b will not cross the point with the same distance from more than 2 base stations.
(For the distance d1 and d2, if fabs(d1-d2)<1e-7, we think d1 == d2).
And every city exactly receive signal from just one base station.
思路:每次获取中点的近期的基站的id,id跟哪边不一样就往哪边递归查找。
#include <stdio.h>
#define INF 99999999 int n,m,ans;
bool vis[50];
double sx[50],sy[50],ex[50],ey[50]; int get(double x,double y)//获取离该点近期的基站
{
double mn=INF;
int id,i; for(i=0;i<m;i++)
{
if((x-ex[i])*(x-ex[i])+(y-ey[i])*(y-ey[i])<mn)
{
mn=(x-ex[i])*(x-ex[i])+(y-ey[i])*(y-ey[i]);
id=i;
}
} return id;
} void dfs(int l,int r,double lx,double ly,double rx,double ry)//递归查找,每次获取中点的近期的基站的id,id跟哪边不一样就往哪边找。 {
if((lx-rx)*(lx-rx)+(ly-ry)*(ly-ry)<1e-14) return; double x=(lx+rx)/2.0;
double y=(ly+ry)/2.0; int id=get(x,y); if(!vis[id])
{
vis[id]=1;
ans++;
} if(id!=l) dfs(l,id,lx,ly,x,y); if(id!=r) dfs(id,r,x,y,rx,ry);
} int main()
{
int i,q,id1,id2;
int from,to; while(~scanf("%d%d",&n,&m))
{
for(i=0;i<n;i++) scanf("%lf%lf",&sx[i],&sy[i]); for(i=0;i<m;i++) scanf("%lf%lf",&ex[i],&ey[i]); scanf("%d",&q); while(q--)
{
scanf("%d%d",&from,&to); id1=get(sx[from-1],sy[from-1]);
id2=get(sx[to-1],sy[to-1]); if(id1==id2) printf("0\n");
else
{
for(i=0;i<m;i++) vis[i]=0;
vis[id1]=1;
vis[id2]=1;
ans=1;
dfs(id1,id2,sx[from-1],sy[from-1],sx[to-1],sy[to-1]);
printf("%d\n",ans);
}
}
}
}
HDU-4643-GSM(DFS)的更多相关文章
- hdu 4643 GSM 计算几何 - 点线关系
/* hdu 4643 GSM 计算几何 - 点线关系 N个城市,任意两个城市之间都有沿他们之间直线的铁路 M个基站 问从城市A到城市B需要切换几次基站 当从基站a切换到基站b时,切换的地点就是ab的 ...
- HDU 4643 GSM 算术几何
当火车处在换基站的临界点时,它到某两基站的距离相等.因此换基站的位置一定在某两个基站的中垂线上, 我们预处理出任意两基站之间的中垂线,对于每次询问,求询问线段与所有中垂线的交点. 检验这些交点是否满足 ...
- HDU 4643 GSM (2013多校5 1001题 计算几何)
GSM Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submiss ...
- hdu 4643 GSM(暴力)
GSM Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Submis ...
- HDU 4643 GSM 简单计算几何
今天比赛的时候略坑, admin告诉我询问Q的个数不超过n^2, 赛后敲了个 O(Q*m^3)的复杂度,但这个复杂度常数比较低,可能在除以个小常数, 300ms过了,真心无语,数据应该水了吧,比赛的时 ...
- HDU 4643 GSM 暑期多校联合训练第五场 1001
点击打开链接 我就不说官方题解有多坑了 V图那么高端的玩意儿 被精度坑粗翔了 AC前 AC后 简直不敢相信 只能怪自己没注意题目For the distance d1 and d2, if fabs( ...
- HDU.5692 Snacks ( DFS序 线段树维护最大值 )
HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...
- hdu 5727 Necklace dfs+二分图匹配
Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...
- hdu 4499 Cannon dfs
Cannon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4499 D ...
- hdu 1175 连连看 DFS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1175 解题思路:从出发点开始DFS.出发点与终点中间只能通过0相连,或者直接相连,判断能否找出这样的路 ...
随机推荐
- No-1.第一个 Python 程序
1. 第一个 HelloWorld 程序 1.1 Python 源程序的基本概念 Python 源程序就是一个特殊格式的文本文件,可以使用任意文本编辑软件做 Python 的开发 Python 程序的 ...
- Syslinux编译环境配置简要步骤
由于毕业设计要做一个加密U盘的LINUX,需要修改一sysylinux引导程序,在网上很少有关于syslinux编译环境配置的文章,在此简要总结一下,发出来共享. 需要的软件: 1. vmware ...
- get data from splunk
link: http://dev.splunk.com/view/python-sdk/SP-CAAAER5 download SDK & setup with python code: im ...
- Maven 的相关配置
第一步: 官方下载路径: http://maven.apache.org/download.cgi maven官方网站:http://www.mvnrepository.com/ 第二步: 请下载这个 ...
- LeetCode (45) Jump Game II
题目 Given an array of non-negative integers, you are initially positioned at the first index of the a ...
- Symmetry
Description The figure shown on the left is left-right symmetric as it is possible to fold the sheet ...
- 笔记——python语言规范
Lint 对你的代码运行pylint 定义: pylint是一个在Python源代码中查找bug的工具. 对于C和C++这样的不那么动态的(译者注: 原文是less dynamic)语言, 这些bug ...
- SPOJ GSS6 Can you answer these queries VI
Can you answer these queries VI Time Limit: 2000ms Memory Limit: 262144KB This problem will be judge ...
- markman & psd
markman & psd MarkMan 设计稿标 & 测量神器 http://www.getmarkman.com/ https://www.jianshu.com/p/83af3 ...
- SPOJ-BRCKTS (括号序列,线段树)
维护括号序列 Replace(i): 将第i个位置的括号反向. Check:测试当前序列是否合法. 题解 将左括号定为1,右括号定为-1,所以只需要满足前缀和序列没有负数即可,即最小值 为正即可,第i ...