bzoj 2304

原创 2016年06月01日 01:35:49

不多说。。。放组数据:

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;
}

相关文章推荐

bzoj 2304: [Apio2011]寻路 (最短路+建图)

题目描述传送门题解对于矩形的每个顶点找到能到达的其他矩形边界上的点,然后将所有有用的点找出来。 先按照纵坐标排序,然后将纵坐标相同的点中相邻的点连边,注意在边界上的点需要特判,因为不能进入矩形。 ...

bzoj 2304 [Apio 2011 path]

喜闻乐见的最短路

【题解】atcoder2304 Cleaning

题目链接 题意:给定一棵带点权的树,允许进行如下操作:选取2个不同的叶子u、v,将u到v的路径所经过的点的点权减1。问能否经过有限次操作使得全体点权变为0。 分析:取一非叶结点作为树的根。设结点u...
  • ZJZNKU
  • ZJZNKU
  • 2017年02月09日 16:35
  • 150

HDU 2304 Electrical Outlets

好水好水

杭电 2304

New~ 2015年杭电ACM暑期集训队的选拔结果公示 欢迎“热爱编程”的高考少年——报考杭州电子科技大学计算机学院 Electrical Outlets Time Limit: 5000...

ORA-2304:不同表空间不同用户的Type及Clob导入问题

今天有同事导入数据库时,当create type时候报错,我在原有数据库A(同一数据库下建立多个用户)测试很多遍都出现这个问题。新创建数据B导入没有问题。 查看ora-02304错误原因...

HDU2304:Electrical Outlets

Problem Description Roy has just moved into a new apartment. Well, actually the apartment itself is...

翻书效果jiaoben2304

  • 2016年04月26日 11:29
  • 1.06MB
  • 下载

新旋风2304下载 最好用繁荣

  • 2010年08月11日 13:03
  • 193KB
  • 下载

bzoj1798【ahoi2009】维护序列

加法和乘法双标记线段树
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:bzoj 2304
举报原因:
原因补充:

(最多只允许输入30个字)