hdu 4885 TIANKENG’s travel(bfs)
题目链接:hdu 4885 TIANKENG’s travel
题目大意:给定N,L,表示有N个加油站,每次加满油能够移动距离L,必须走直线,可是能够为斜线。然后给出sx,sy,ex,ey,以及N个加油站的位置,问说最少经过几个加油站,路过不加油也算。
解题思路:一開始以为经过能够不算,所以o(n2)的复杂度建图,然后用bfs求最短距离,结果被FST了。
将点依照x坐标排序,这样在建图是保证当前点为最左点,每次建立一条边的时候,将该边的斜率记录,下次有同样的斜率则不加边,斜率能够用两个整数表示,可是要注意化简成最简。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1005;
struct point {
int id;
ll x, y;
}p[maxn], s, e;
ll L;
int N, d[maxn];
vector<int> g[maxn];
set<pii> vis;
inline ll gcd (ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
inline bool cmp (const point& a, const point& b) {
return a.x < b.x;
}
inline ll dis (ll x, ll y) {
return x * x + y * y;
}
bool search (ll x, ll y) {
ll d = gcd(x, y);
if (d < 0)
d = -d;
x /= d; y /= d;
if (vis.find(make_pair(x, y)) != vis.end())
return true;
vis.insert(make_pair(x, y));
return false;
}
void addEdge (point a, point b) {
ll d = dis(a.x - b.x, a.y - b.y);
if (d <= L && !search(b.x - a.x, b.y - a.y)) {
g[a.id].push_back(b.id);
g[b.id].push_back(a.id);
//printf("%d %d %lld %lld\n", a.id, b.id, d, L * L);
}
}
void init () {
scanf("%d%lld", &N, &L);
scanf("%lld%lld%lld%lld", &p[0].x, &p[0].y, &p[1].x, &p[1].y);
p[0].id = 0;
p[1].id = 1;
N += 2;
L = L * L;
for (int i = 0; i < N; i++)
g[i].clear();
for (int i = 2; i < N; i++) {
scanf("%lld%lld", &p[i].x, &p[i].y);
p[i].id = i;
}
sort(p, p + N, cmp);
for (int i = 0; i < N; i++) {
vis.clear();
for (int j = i + 1; j < N; j++)
addEdge(p[i], p[j]);
}
}
void bfs () {
queue<int> que;
que.push(0);
memset(d, -1, sizeof(d));
d[0] = 0;
while (!que.empty()) {
int u = que.front();
que.pop();
if (u == 1) {
printf("%d\n", d[u]-1);
return;
}
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (d[v] == -1) {
d[v] = d[u] + 1;
que.push(v);
}
}
}
printf("impossible\n");
}
int main () {
int cas;
scanf("%d", &cas);
while (cas--) {
init();
bfs();
}
return 0;
}
hdu 4885 TIANKENG’s travel(bfs)的更多相关文章
- HDU 2717 Catch That Cow --- BFS
HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...
- 【HDU】4418 Time travel
http://acm.hdu.edu.cn/showproblem.php?pid=4418 题意:一个0-n-1的坐标轴,给出起点X.终点Y,和初始方向D(0表示从左向右.1表示从右向左,-1表示起 ...
- HDU 5876 关于补图的bfs
1.HDU 5876 Sparse Graph 2.总结:好题,把STL都过了一遍 题意:n个点组成的完全图,删去m条边,求点s到其余n-1个点的最短距离. 思路:把点分为两个集合,A为所有没有到达 ...
- HDU 4435 charge-station () bfs图论问题
E - charge-station Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- hdu 1240:Asteroids!(三维BFS搜索)
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU(1175),连连看,BFS
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1175 越学越不会,BFS还是很高级的. 连连看 Time Limit: 20000/100 ...
- HDU 3085 Nightmare II 双向bfs 难度:2
http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...
- hdu - 1240 Nightmare && hdu - 1253 胜利大逃亡(bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1240 开始没仔细看题,看懂了发现就是一个裸的bfs,注意坐标是三维的,然后每次可以扩展出6个方向. 第一维代表在 ...
- hdu - 1180 诡异的楼梯 (bfs+优先队列)
http://acm.hdu.edu.cn/showproblem.php?pid=1180 注意点就是楼梯是在harry移动完之后才会改变方向,那么只要统计到达这个点时间奇偶性,就可以知道当前楼梯是 ...
随机推荐
- mrtg监控网络流量简单配置
Mrtg服务器搭建(监控网络流量) [日期:2012-07-03] 来源:Linux社区 作者:split_two [字体:大 中 小] [实验环境] 监控机:Red Hat linux 5.3 ...
- hdoj 3018 Ant Trip(无向图欧拉路||一笔画+并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 思路分析:题目可以看做一笔画问题,求最少画多少笔可以把所有的边画一次并且只画一次: 首先可以求出 ...
- hdoj 1028 Ignatius and the Princess III(区间dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1028 思路分析:该问题要求求出某个整数能够被划分为多少个整数之和(如 4 = 2 + 2, 4 = 2 ...
- 2014年去哪儿网笔试题--有两个文件context.txt和words.conf,请尝试将他们合并成为一段文字,并打印出来。
有两个文件context.txt和words.conf,请尝试将他们合并成为一段文字,并打印出来. 这两个文件内容如下: context.txt “并不是每个人都需要$(qunar)自己的粮食,$(f ...
- python实现的文本编辑器 - Skycrab - 博客频道 - CSDN.NET
Download Qt, the cross-platform application framework | Qt Project Qt 5.2.1 for Windows 64-bit (VS 2 ...
- Nutch 二次开发之parse正文内容
关于nutch的基础知识能够參考lemo的专栏 nutch支持二次开发,为了满足搜索的准确率的问题,考虑只将网页正文的内容提取出来作为索引的内容,相应的是parse_text的数据.我使用的事nutc ...
- C#中继承,集合(Eleventh day)
又到了总结知识的时间,今天在云和学院继续学习了继承的一些运用,和集合的运用.下面就总结下来吧 理论: 显示调用父类的构造方法,关键字: base:构造函数不能被继承:子类对象被实例化的时候会先去主动的 ...
- linux下移动或者复制文件覆盖相同文件夹时,文件夹里面的每个文件都提示是否覆盖
链接地址:http://blog.chinaunix.net/uid-23683795-id-2391087.html # vi ~/.bashrc 如果你看到如下内容,以下命令都会用别名执行了, ...
- BZOJ 1627: [Usaco2007 Dec]穿越泥地( BFS )
BFS... --------------------------------------------------------------------------------------- #incl ...
- Python之路Day19
志不坚者智不达. 主要内容:Django分页.自定义simpletag.权限管理 Django分页 Django自带了一个分页功能,使用起来很方便.官方文档 先来看一下效果图: 后台代码: def c ...