题目链接: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)的更多相关文章

  1. 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,最先 ...

  2. 【HDU】4418 Time travel

    http://acm.hdu.edu.cn/showproblem.php?pid=4418 题意:一个0-n-1的坐标轴,给出起点X.终点Y,和初始方向D(0表示从左向右.1表示从右向左,-1表示起 ...

  3. HDU 5876 关于补图的bfs

    1.HDU 5876  Sparse Graph 2.总结:好题,把STL都过了一遍 题意:n个点组成的完全图,删去m条边,求点s到其余n-1个点的最短距离. 思路:把点分为两个集合,A为所有没有到达 ...

  4. HDU 4435 charge-station () bfs图论问题

    E - charge-station Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  5. hdu 1240:Asteroids!(三维BFS搜索)

    Asteroids! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. HDU(1175),连连看,BFS

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1175 越学越不会,BFS还是很高级的. 连连看 Time Limit: 20000/100 ...

  7. HDU 3085 Nightmare II 双向bfs 难度:2

    http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...

  8. hdu - 1240 Nightmare && hdu - 1253 胜利大逃亡(bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1240 开始没仔细看题,看懂了发现就是一个裸的bfs,注意坐标是三维的,然后每次可以扩展出6个方向. 第一维代表在 ...

  9. hdu - 1180 诡异的楼梯 (bfs+优先队列)

    http://acm.hdu.edu.cn/showproblem.php?pid=1180 注意点就是楼梯是在harry移动完之后才会改变方向,那么只要统计到达这个点时间奇偶性,就可以知道当前楼梯是 ...

随机推荐

  1. [转载]cin、cin.get()、cin.getline()、getline()、gets()函数的用法

    1.cin>>           用法1:最基本,也是最常用的用法,输入一个数字: #include <iostream>using namespace std;main ( ...

  2. hdu1711(终于搞懂了KMP算法了。。)

    题意:给你两个长度分别为n(1 <= N <= 1000000)和m(1 <= M <= 10000)的序列a[]和b[],求b[]序列在a[]序列中出现的首位置.如果没有请输 ...

  3. NSDate conversion utilities

    // Gets UTC NSDate from DateTime(.Net/WCF). + (NSDate *)fromDateTime:(NSString *)dateTime { NSDate * ...

  4. [译]MDX 介绍

    关于MDX MDX (Multi Dimensional eXpression language) 是非常强大的工具,可以将你的多维数据库/cube 发挥到极致. 本文会覆盖MDX基础,并且希望能使你 ...

  5. 关于Repeater中使用DorpWownList的问题

    关于Repeater中使用DorpWownList的问题 前台: <asp:Repeater ID="Repeater1" runat="server" ...

  6. 由浅到深讲解Mybatis

    Mybatis的简介 什么是Mybatis? Mybatis是支持定制化SQL,存储过程以及高级映射的持久化框架.Mybatis避免了几乎所有的JDBC代码和 手动设置获取结果集.Mybatis可以对 ...

  7. Setting property 'source' to 'org.eclipse.jst.jee.server [问题点数:40分]

    链接地址:http://bbs.csdn.net/topics/390131469 警告: [SetContextPropertiesRule]{Context} Setting property ' ...

  8. 使用JS进行pc端、手机端判断

     <script type="text/javascript">            (function(){                var ua = nav ...

  9. 我的Python成长之路---第六天---Python基础(18)---2016年2月20日(晴)

    os模块 提供对操作系统进行调用的接口 >>> import os >>> os.getcwd() # 获取当前工作目录,类似linux的pwd命令 '/data/ ...

  10. gcc编译4个阶段

    gcc的编译流程分为四个步骤,分别为:· 预处理(Pre-Processing)  -E· 编译(Compiling)         -S· 汇编(Assembling)        -c· 链接 ...