hdu 4885 TIANKENG’s travel(bfs)
来源:互联网 发布:网络服装代理怎么做 编辑:程序博客网 时间:2024/05/12 19:16
题目链接: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;}
2 1
- HDU 4885 TIANKENG’s travel(几何bfs)
- hdu 4885 TIANKENG’s travel(bfs)
- hdu 4885TIANKENG’s travel bfs
- HDU-4885-TIANKENG’s travel
- HDU 4885 TIANKENG’s travel
- 【HDU】4885 TIANKENG’s travel 最短路
- HDU 4885 TIANKENG’s travel 最短路
- HDU 4885 —— TIANKENG’s travel(最短路)
- HDU 4885 Bestcoder#2-1003 TIANKENG’s travel 【判断三点共线建图 + 最短路bfs】
- 【最短路】 HDOJ 4885 TIANKENG’s travel
- hdu 4885 TIANKENG’s travel (最短路+判断三点共线)
- [ACM] HDU 4885 TIANKENG’s travel (特殊建图,最短路)
- hdu TIANKENG’s restaurant
- TIANKENG’s restaurant HDU
- HDU 4883 TIANKENG’s restaurant
- hdu 4883 TIANKENG’s restaurant
- HDU 4883 TIANKENG’s restaurant
- hdu 4883 TIANKENG’s restaurant
- openfire插件之PacketInterceptor
- 判断该应用是否被安装
- 如何使用 ALT 键输入特殊符号
- 关于核心竞争力-技术路线的思考
- JVM内存分配和回收图文详解
- hdu 4885 TIANKENG’s travel(bfs)
- mac上安装svn插件
- Oracle 卸载
- AIX系统不能正确识别vpath和hdisk设备,如何解决?
- 华为HCNA安全培训认证要多少钱?
- Android开发:判断应用第一次启动(精简代码版)
- javascript参数传递字符串
- ”Java 方法可以返回数组类型“ 所想到的
- Java代码 自定义Android RatingBar 样式 【调整大小】