UOJ 184 [ZJOI2016]旅行者

离线分治,最短路

这一篇说得很清楚了,时间复杂度也有分析:
http://blog.csdn.net/neither_nor/article/details/51733997

UOJ上的点实在是恶心。普通最短路不能过,发现每一次分治下去要连续跑最短路,前一次最短路的结果可以帮助下一次。然后套上堆优化dijkstra可卡过。听说用SPFA+SLF,LLL也可过?

BZOJ网站昨天就挂了,有空再上去交一下4456

#include<cstdio>
#include<cstring>
#include<algorithm>
#define Q 100005
#define MAX 20005
#define reg register
#define cmin(_i,_j) _i>_j?_i=_j:0 
#define pack(_i,_j) ((_i-1)*m+_j)
#define add2(_i,_j,_k) {add(_i,_j,_k);add(_j,_i,_k);}
#define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
char B[1 << 15], *S = B, *T = B;
using namespace std;
int in()
{
    reg int r=0;
    reg char c=getc();
    while(c<'0'||c>'9')c=getc();
    while(c>='0'&&c<='9')r=r*10+c-'0',c=getc();
    return r;
}
struct edge{int next,to,val;}e[MAX*5];
struct node
{
    int x, dis;
    node(){}
    node(int a, int b):x(a),dis(b){}
    friend bool operator < (node a, node b)
    {
        return a.dis < b.dis;
    }
}heap[MAX*5];
const int INF = 1<<29;
int ecnt, n, m, q, rx[MAX], ry[MAX], last[MAX], dis[MAX], tot, ans[Q], vis[MAX], timer, qq[MAX];
bool inq[MAX];
struct query
{
    int x1, y1, x2, y2, ans, id;
}que[Q], temp[Q];
void heap_add(node x)
{
    heap[++tot]=x;
    for(int i = tot, j = i>>1; j; i=j, j>>=1)
    {
        if(heap[i]<heap[j])
            swap(heap[i],heap[j]);
        else break;
    }
}
void heap_pop()
{
    heap[1]=heap[tot--];
    for(int i = 1, j = i<<1; j<=tot; i=j, j<<=1)
    {
        if(j<tot && heap[j|1]<heap[j])j|=1;
        if(heap[j]<heap[i])
            swap(heap[i],heap[j]);
        else break;
    }
}
void add(int a, int b, int c)
{
    e[++ecnt]=(edge){last[a],b,c};
    last[a]=ecnt;
}
void init()
{
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
        {
            int k = pack(i,j);
            rx[k]=i;
            ry[k]=j;
        }
}
void dijk(int s, int x1, int y1, int x2, int y2, bool first)
{
    timer++;
    qq[0]=s;
    heap_add(node(s,dis[s]=0));
    memset(dis,63,sizeof(dis));
    dis[s]=0;
    int cnt=((x2-x1+1)*(y2-y1+1));
    while(tot)
    {
        int x=heap[1].x;
        heap_pop();
        if(vis[x]==timer)continue;
        cnt--;
        vis[x]=timer;
        for(reg int i = last[x]; i; i=e[i].next)
        {
            int y=e[i].to;
            if(vis[y]==timer||rx[y]<x1||rx[y]>x2||ry[y]<y1||ry[y]>y2)continue;
            if(dis[x]+e[i].val<dis[y])
            {
                dis[y]=dis[x]+e[i].val;
                heap_add(node(y,dis[y]));
            }
        }
        if(!cnt)tot=cnt;
    }
}
void solve(int x1, int y1, int x2, int y2, int l, int r)
{
    if(l>r)return;
    if(x2-x1 <= y2-y1)
    {
        int mid=(y1+y2)>>1;
        for(reg int i = x1; i <= x2; i++)
        {
            dijk(pack(i,mid),x1,y1,x2,y2,i==x1);
            for(reg int j = l; j <= r; j++)
                cmin(que[j].ans, dis[pack(que[j].x1,que[j].y1)]+dis[pack(que[j].x2,que[j].y2)]);
        }
        int nl=l-1, nr=r+1;
        for(reg int i = l; i <= r; i++)
        {
            if(que[i].y1<mid && que[i].y2<mid)temp[++nl]=que[i];
            else if(que[i].y1>mid && que[i].y2>mid)temp[--nr]=que[i];
            cmin(ans[que[i].id],que[i].ans);
        }
        for(reg int i = l; i <= r; i++)que[i]=temp[i];
        solve(x1,y1,x2,mid-1,l,nl);
        solve(x1,mid+1,x2,y2,nr,r);
    }
    else
    {
        int mid=(x1+x2)>>1;
        for(reg int i = y1; i <= y2; i++)
        {
            dijk(pack(mid,i),x1,y1,x2,y2,i==y1);
            for(int j = l; j <= r; j++)
                cmin(que[j].ans, dis[pack(que[j].x1,que[j].y1)]+dis[pack(que[j].x2,que[j].y2)]);
        }
        int nl=l-1, nr=r+1;
        for(reg int i = l; i <= r; i++)
        {
            if(que[i].x1<mid && que[i].x2<mid)temp[++nl]=que[i];
            else if(que[i].x1>mid && que[i].x2>mid)temp[--nr]=que[i];
            cmin(ans[que[i].id],que[i].ans);
        }
        for(reg int i = l; i <= r; i++)que[i]=temp[i];
        solve(x1,y1,mid-1,y2,l,nl);
        solve(mid+1,y1,x2,y2,nr,r);
    }
}
int main()
{
    n=in(), m=in();
    init();
    for(reg int i = 1, v; i <= n; i++)
        for(reg int j = 1; j < m; j++)
        {
            v=in();
            add2(pack(i,j),pack(i,j+1),v);
        }
    for(reg int i = 1, v; i < n; i++)
        for(reg int j = 1; j <= m; j++)
        {
            v=in();
            add2(pack(i,j),pack(i+1,j),v);  
        }
    q=in();
    for(reg int i = 1; i <= q; i++)
    {
        que[i].x1=in(); que[i].y1=in(); 
        que[i].x2=in(); que[i].y2=in();
        que[i].ans=INF;
        que[i].id=i;
    }
    memset(ans,63,sizeof(ans)); 
    solve(1,1,n,m,1,q);
    for(reg int i = 1; i <= q; i++)
        printf("%d\n",ans[i]);
}
发布了325 篇原创文章 · 获赞 100 · 访问量 12万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 像素格子 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览