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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 异地恋要分手了怎么办 异地恋没话题了怎么办 陪婆婆聊天心情超级郁闷怎么办? 他不想理你了怎么办 陌陌看到信息不回怎么办 qq的文档看不了怎么办 怀孕了分手了怎么办啊 qq节日祝福关了怎么办 微信欠款不还怎么办 qq文件记录删除了怎么办 qq漫游记录删了怎么办 换了新手机微信怎么办 新手机了微信怎么办 微信收藏空间已满怎么办 微信收藏空间满了怎么办 qq步数上传不了怎么办 qq发送的文件失效了怎么办 苹果手机微信数据损坏怎么办 微信文件被清理怎么办 老婆与别人聊暧昧话题怎么办 微信不小心删了聊天记录怎么办 当聊天没话题了怎么办 老婆和别人频繁聊天老公怎么办 qq上把人屏蔽了怎么办 注册微信验证码发不出去怎么办 两个人在一起没有话题怎么办 两个人聊天没话题怎么办 qq邮箱限制信用卡账单怎么办 和朋友聊天没话题怎么办 跟朋友聊天没话题怎么办 和朋友聊天找不到话题怎么办 qq加好友忽略了怎么办 微信加好友收不到验证消息怎么办 陌陌距离乱了怎么办 qq不能点赞了怎么办 快递发货地址写错了怎么办 订的海鲜不发货怎么办 拉人进群频繁了怎么办 qq群邀请过于频繁怎么办 qq一直被拉进群怎么办 qq号被冻结解封不了怎么办