关闭

bzoj 2304

214人阅读 评论(0) 收藏 举报
分类:

不多说。。。放组数据:

path.in

10

2 4 6 10
1
2 4 6 10

2 7 6 8
1
2 4 6 10

1 7 7 8 
2 
2 5 3 8 
4 10 6 7  

2 1 5 4 
1 
3 1 4 3 

2 7 6 8
1
2 4 6 10

1 7 7 8 
2 
2 5 3 8 
4 10 6 7 

2 1 5 4 
1 
3 1 4 3 

2 7 6 8
1
2 4 6 10

1 7 7 8 
2 
2 5 3 8 
4 10 6 7 

2 1 5 4 
1 
3 1 4 3 

------------------------------------

path.out

10
9
9
No Path
9
9
No Path
9
9
No Path

总之。。。比以前写的代码清晰多了。。。

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <utility>
#include <queue>
#include <map>
#include <cstring>

const int maxn = 100000;
const int inf = 0x3f3f3f3f;
const long long linf = 0x3f3f3f3f3f3f3f3fll;

struct Node
{
    int x, y;

    friend bool operator < (const Node &a,const Node &b)
    {
        return a.x != b.x ? a.x < b.x : a.y < b.y;
    }
};

struct Square
{
    Node ll, rr;
};

struct Event
{
    int tp, x, l, r;

    friend bool operator < (const Event &a,const Event &b)
    {
        return a.x != b.x ? a.x < b.x : a.tp > b.tp;
    }
};

struct Data
{
    long long w;
    int ind;

    friend bool operator < (const Data &a,const Data &b)
    {
        return a.w > b.w;
    }
};

int n;
std::map<Node,int> ind;
int tot;
bool fo[maxn][4];
Node P[maxn];

Square S[maxn];
int Tx[maxn];

struct Edge
{
    int v, w, next;

} edge[maxn];

int head[maxn], el;

Node trans(Node p,int t)
{
    if(t < 0)
    {
        if((-t) & 1) p.x *= -1;
        if((-t) & 2) std::swap(p.x, p.y);
    }

    if(t > 0)
    {
        if(t & 2) std::swap(p.x, p.y);
        if(t & 1) p.x *= -1;
    }

    return p;
}

int newnode(Node p)
{
    P[++tot] = p;
    head[tot] = 0;

    memset(fo[tot], false, sizeof(bool) * 4);

    return tot;
}
void newedge(int u,int v,int w)
{
    edge[++el] = (Edge){v, w, head[u]}, head[u] = el;
}
int getind(Node p)
{
    if(!ind.count(p))
    {
        ind[p] = newnode(p);
    }

    return ind[p];
}
void init()
{
    Node s, t;

    ind.clear();
    tot = el = 0;

    scanf("%d%d%d%d", &s.x, &s.y, &t.x, &t.y);
    scanf("%d", &n);

    ind[s] = newnode(s), ind[t] = newnode(t);

    for(int i = 1; i <= n; i++)
    {
        int xl, xr, yl, yr;

        scanf("%d%d%d%d", &xl, &yl, &xr, &yr);

        if(xl > xr) std::swap(xl, xr);
        if(yl > yr) std::swap(yl, yr);

        S[i] = (Square){(Node){xl, yl}, (Node){xr, yr}};

        getind((Node){xl, yl}), getind((Node){xr, yl});
        getind((Node){xl, yr}), getind((Node){xr, yr});
    }
}
void build(int l,int r,int s)
{
    Tx[s] = -inf;   

    if(l != r)
    {
        int mid = (l + r) >> 1;

        build(l, mid, s << 1);
        build(mid + 1, r, s << 1 | 1);
    }
}
void change(int x,int l,int r,int ll,int rr,int s)
{
    if(l <= ll && rr <= r)
    {
        Tx[s] = x;
        return;
    }

    int mid = (ll + rr) >> 1;

    if(l <= mid) change(x, l, r, ll, mid, s << 1);
    if(r > mid) change(x, l, r, mid + 1, rr, s << 1 | 1);
}
int query(int k,int l,int r,int s)
{
    if(l == r) return Tx[s];

    int res = 0;
    int mid = (l + r) >> 1;

    if(k <= mid) res = query(k, l, mid, s << 1);
    else res = query(k, mid + 1, r, s << 1 | 1);

    res = std::max(res, Tx[s]);

    return res;
}
void prework(int type)
{
    static Event E[maxn];
    static int ty[maxn];
    int tl = 2 * n;

    for(int i = 1; i <= n; i++)
    {
        S[i].ll = trans(S[i].ll, type);
        S[i].rr = trans(S[i].rr, type);

        int xl = S[i].ll.x, xr = S[i].rr.x;
        int yl = S[i].ll.y, yr = S[i].rr.y;

        if(xl > xr) std::swap(xl, xr);
        if(yl > yr) std::swap(yl, yr);

        ty[i] = yl, ty[i + n] = yr;

        S[i] = (Square){(Node){xl, yl}, (Node){xr, yr}};            
    }

    ty[++tl] = trans(P[1], type).y;
    ty[++tl] = trans(P[2], type).y;

    std::sort(ty + 1, ty + tl + 1);
    tl = std::unique(ty + 1, ty + tl + 1) - (ty + 1);

    int Ecnt = 0;

    for(int i = 1; i <= n; i++)
    {
        int xl = S[i].ll.x, xr = S[i].rr.x;
        int yl = S[i].ll.y, yr = S[i].rr.y;

        yl = std::lower_bound(ty + 1, ty + tl + 1, yl) - ty;
        yr = std::lower_bound(ty + 1, ty + tl + 1, yr) - ty;

        E[++Ecnt] = (Event){0, xl, yl, yr};
        E[++Ecnt] = (Event){1, xr, yl, yr};
    }

    int sPy = std::lower_bound(ty + 1, ty + tl + 1, trans(P[1], type).y) - ty;
    int tPy = std::lower_bound(ty + 1, ty + tl + 1, trans(P[2], type).y) - ty;

    E[++Ecnt] = (Event){0, trans(P[1], type).x, sPy, sPy};
    E[++Ecnt] = (Event){0, trans(P[2], type).x, tPy, tPy};

    std::sort(E + 1, E + Ecnt + 1);

    build(1, tl, 1);

    for(int i = 1; i <= Ecnt; i++)
    {
        if(E[i].tp == 1)
        {
            change(E[i].x, E[i].l, E[i].r, 1, tl, 1);
        }
        else
        {
            int lx = query(E[i].l, 1, tl, 1);
            int rx = query(E[i].r, 1, tl, 1);

            if(lx > -inf) fo[getind(trans((Node){lx, ty[E[i].l]}, -type))][type] = true;
            if(rx > -inf) fo[getind(trans((Node){rx, ty[E[i].r]}, -type))][type] = true;

        }
    }

    for(int i = 1; i <= n; i++)
    {
        S[i].ll = trans(S[i].ll, -type);
        S[i].rr = trans(S[i].rr, -type);

        int xl = S[i].ll.x, xr = S[i].rr.x;
        int yl = S[i].ll.y, yr = S[i].rr.y;

        if(xl > xr) std::swap(xl, xr);
        if(yl > yr) std::swap(yl, yr);

        S[i] = (Square){(Node){xl, yl}, (Node){xr, yr}};        
    }
}
int getdist(int a,int b)
{
    return abs(P[a].x - P[b].x) + abs(P[a].y - P[b].y);
}
void changefo()
{
    for(int i = 1; i <= n; i++)
    {
        int xl = S[i].ll.x, xr = S[i].rr.x;
        int yl = S[i].ll.y, yr = S[i].rr.y;

        memset(fo[getind((Node){xl, yl})], false, sizeof(bool) * 4);
        memset(fo[getind((Node){xr, yl})], false, sizeof(bool) * 4);
        memset(fo[getind((Node){xl, yr})], false, sizeof(bool) * 4);
        memset(fo[getind((Node){xr, yr})], false, sizeof(bool) * 4);
    }
}
void build(bool revs,int b)
{
    static std::pair<Node,int> pa[maxn];

    for(int i = 1; i <= tot; i++)
    {
        pa[i] = std::make_pair(P[i], i);

        if(revs) std::swap(pa[i].first.x, pa[i].first.y);
    }

    std::sort(pa + 1, pa + tot + 1);

    for(int i = 1, j = 1; i <= tot; i = j)
    {
        while(j <= tot && pa[i].first.x == pa[j].first.x) j++;

        for(int k = i + 1; k < j; k++)
        {
            int pre = pa[k - 1].second;
            int now = pa[k].second;
            int w = getdist(now, pre);

            if(!fo[now][b]) newedge(now, pre, w);

            if(!fo[pre][b ^ 1]) newedge(pre, now, w);
        }
    }
}
void solve()
{
    static long long dist[maxn];
    static std::priority_queue<Data> heap;
    static bool vis[maxn];

    for(int i = 1; i <= tot; i++)
    {
        dist[i] = linf;
        vis[i] = false;
    }

    dist[1] = 0;
    heap.push((Data){0, 1});

    while(!heap.empty())
    {
        int t = heap.top().ind;

        heap.pop();

        if(vis[t]) continue;

        vis[t] = true;

        for(int i = head[t]; i ; i = edge[i].next)
        {
            int v = edge[i].v;

            if(dist[t] + edge[i].w < dist[v])
            {
                dist[v] = dist[t] + edge[i].w;
                heap.push((Data){dist[v], v});
            }
        }
    }

    if(dist[2] < linf)
    {
        printf("%lld\n", dist[2]);
    }
    else
    {
        puts("No Path");
    }
}
int main()
{
    int T;

#ifndef ONLINE_JUDGE
    freopen("path.in","r",stdin);
    freopen("path.out","w",stdout);
#endif

    scanf("%d", &T);

    while(T--)
    {
        init();

        prework(0);
        prework(1);
        prework(2);
        prework(3);

        changefo();

        build(true, 0);
        build(false, 2);

        solve();
    }

#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:60029次
    • 积分:2701
    • 等级:
    • 排名:第13908名
    • 原创:206篇
    • 转载:10篇
    • 译文:0篇
    • 评论:13条
    文章分类