2018 徐州网络赛 J

  • 131072K

After the long vacation, the maze designer master has to do his job. A tour company gives him a map which is a rectangle. The map consists of N \times MN×M little squares. That is to say, the height of the rectangle is NN and the width of the rectangle is MM. The master knows exactly how the maze is going to use. The tour company will put a couple in two different squares in the maze and make them seek each other. Of course,the master will not make them find each other easily. The only thing the master does is building some wall between some little squares. He knows in that way, wherever the couple is put, there is only one path between them. It is not a difficult thing for him, but he is a considerate man. He also knows that the cost of building every wall between two adjacent squares is different(Nobody knows the reason). As a result, he designs the maze to make the tour company spend the least money to build it.

Now, here‘s your part. The tour company knows you‘re the apprentice of the master, so they give you a task. you‘re given QQ qustions which contain the information of where the couple will be put. You need to figure out the length of the shortest path between them.

However,the master doesn‘t tell you how he designs the maze, but he believes that you, the best student of himself, know the way. So he goes on vacation again.

Input

The first line of the input contains two integers NN and MM (1 \le N,M \le 5001≤N,M≤500), giving the number of rows and columns of the maze.

The next N \times MN×M lines of the input give the information of every little square in the maze, and their coordinates are in order of (1,1)(1,1) , (1,2)(1,2) \cdots? (1,M)(1,M) , (2,1)(2,1) , (2,2)(2,2) , \cdots? , (2,M)(2,M) , \cdots? ,(N,M)(N,M).

Each line contains two characters DD and RR and two integers aa , bb (0 \le a,b \le 20000000000≤a,b≤2000000000 ), aa is the cost of building the wall between it and its lower adjacent square, and bb is the cost of building the wall between it and its right adjacent square. If the side is boundary, the lacking path will be replaced with X 00.

The next line contains an integer QQ (1 \le Q \le 1000001≤Q≤100000 ), which represents the number of questions.

The next QQ lines gives four integers, x_1x1?, y_1y1?, x_2x2?, y_2y2? ( 1 \le x_11≤x1? , x_2 \le Nx2?≤N , 1 \le y_11≤y1? , y_2 \le My2?≤M ), which represent two squares and their coordinate are (x_1x1? , y_1y1?) and (x_2x2? , y_2y2?).

(xx,yy) means row xx and column yy.

It is guaranteed that there is only one kind of maze.

Output

For each question, output one line with one integer which represents the length of the shortest path between two given squares.

样例输入复制

3 3
D 1 R 9
D 7 R 8
D 4 X 0
D 2 R 6
D 12 R 5
D 3 X 0
X 0 R 10
X 0 R 11
X 0 X 0
3
1 1 3 3
1 2 3 2
2 2 3 1

样例输出复制

4
2
2

题目来源

ACM-ICPC 2018 徐州赛区网络预赛

思路:

最大生成树+lca

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> pl;
const int maxn=550;
const int maxm=maxn*maxn;
int anc[maxm][20],fa[maxm],dep[maxm];
std::vector<int> vec[maxm];
std::vector<pl> e;
inline int fnd(int x) {
    return x==fa[x]?x:fa[x]=fnd(fa[x]);
}
void dfs(int x,int fa=0) {
    anc[x][0]=fa;
    for (int i=1; (~anc[x][i-1]) && (~anc[anc[x][i-1]][i-1]); ++i)
        anc[x][i]=anc[anc[x][i-1]][i-1];
    for (auto y:vec[x]) {
        if(y==fa) continue;
        dep[y]=dep[x]+1;
        dfs(y,x);
    }
}
int lca(int x,int y) {
    if(dep[x]>dep[y]) swap(x,y);
    for (int i=19; i>=0; --i) {
        if(~anc[y][i] && dep[x]<=dep[anc[y][i]])
            y=anc[y][i];
    }
    if(x==y) return x;
    for (int i=19; i>=0; --i) {
        if(anc[x][i]!=anc[y][i]) {
            x=anc[x][i];
            y=anc[y][i];
        }
    }
    return anc[x][0];
}
int n,m,q;
inline void init() {
    for (int i=0; i<maxm; i++) fa[i]=i;
    memset(anc,-1,sizeof(anc));
    memset(dep,0,sizeof(dep));
}
inline int rt(int x,int y) {
    return x*m+y;
}
char s[10];
int a,b;
int main() {
//    freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&m);
    init();
//    iota(fa,fa+maxm);
//    puts("1");
    for (int i=1; i<=n; ++i) {
        for (int j=1; j<=m; ++j) {
            scanf("%s%d%s%d",s,&a,s,&b);
            if(i<n) e.push_back(pl(a,P(rt(i,j),rt(i+1,j))));
            if(j<m) e.push_back(pl(b,P(rt(i,j),rt(i,j+1))));
        }
    }
    sort(e.begin(),e.end(),[](pl x,pl y) {        return x.first>y.first;    });
    for (int i=0; i<e.size(); ++i) {
        P p=e[i].second;
        int x=p.first,y=p.second;
        int fx=fnd(x),fy=fnd(y);
        if(fx!=fy) {
            vec[x].push_back(y);
            vec[y].push_back(x);
            fa[fx]=fy;
        }
    }
    dfs(rt(1,1));
    scanf("%d",&q);
    int x,y,xx,yy;
    while(q--) {
        scanf("%d%d%d%d",&x,&y,&xx,&yy);
        int p=rt(x,y),q=rt(xx,yy);
        int la=lca(p,q);
//        printf("%d %d %d\n",dep[p],dep[q],dep[la]);
        printf("%d\n",dep[p]+dep[q]-2*dep[la]);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/acerkoo/p/9638107.html

时间: 2024-08-01 04:20:44

2018 徐州网络赛 J的相关文章

2018徐州网络赛H. Ryuji doesn&#39;t want to study

题目链接: https://nanti.jisuanke.com/t/31458 题解: 建立两个树状数组,第一个是,a[1]*n+a[2]*(n-1)....+a[n]*1;第二个是正常的a[1],a[2],a[3]...a[n] #include "bits/stdc++.h" using namespace std; #define ll long long const int MAXN=1e5+10; ll sum[MAXN],ans[MAXN]; ll num[MAXN];

ACM-ICPC 2018徐州网络赛-H题 Ryuji doesn&#39;t want to study

C*M....死于update的一个long long写成int了 心累 不想写过程了 ******** 树状数组,一个平的一个斜着的,怎么斜都行 题库链接:https://nanti.jisuanke.com/t/31460 #include <iostream> #include <cstring> #define ll long long #define lowbit(x) (x & -x) using namespace std; const int maxn =

2018 徐州网络赛A

262144K After Incident, a feast is usually held in Hakurei Shrine. This time Reimu asked Kokoro to deliver a Nogaku show during the feast. To enjoy the show, every audience has to wear a Nogaku mask, and seat around as a circle. There are N guests Re

2018 徐州网络赛 H

Ryuji is not a good student, and he doesn't want to study. But there are n books he should learn, each book has its knowledge a[i]a[i]. Unfortunately, the longer he learns, the fewer he gets. That means, if he reads books from ll to rr, he will get a

2018沈阳网络赛J

给一颗树,两种操作,一种把同一层的点权值加上v,另一种求一点下的子树权值和. 按层数中点个数分块,小块直接暴力把所有点用bit更新,大块把层的值存下来. 询问的时候子树权值和为bit中的值以及其下面的点在大块中的值,下面中的点在大块中的值用二分实现. #include <bits/stdc++.h> #include <unordered_set> #include <unordered_map> #define pb push_back #define mp make

2018焦作网络赛J

不知道题意,队友用java大数+二分过了? import java.util.Arrays; import java.util.Scanner; import java.io.*; import java.math.*; public class Main { static boolean check(BigInteger num,BigInteger n) { boolean flag = true; BigInteger a = new BigInteger("0"); BigIn

ICPC 2018 南京网络赛 J Magical Girl Haze(多层图最短路)

传送门:https://nanti.jisuanke.com/t/A1958 题意:n个点m条边的路,你有k次机会将某条路上的边权变为0,问你最短路径长度 题解:最短路变形,我们需要在常规的最短路上多开 一维,记录,我消耗j次机会时的最短路径长度为多少 代码: /** * ┏┓ ┏┓ * ┏┛┗━━━━━━━┛┗━━━┓ * ┃ ┃ * ┃ ━ ┃ * ┃ > < ┃ * ┃ ┃ * ┃... ⌒ ... ┃ * ┃ ┃ * ┗━┓ ┏━┛ * ┃ ┃ Code is far away fro

【2018徐州网络赛】Morgana Net (矩阵快速幂)

给你一个n*n的矩阵A,和一个m*m的矩阵B(m%2==1) B是卷积核,让你用B对A做t次卷积运算,并且对于A中的每一个元素计算出来的值要模2,所以A最后会是一个01矩阵. 问你经过t此后,A中有多少个元素=1 1<=t<=1e9,1<=n<=8,1<=m<=n SOLUTION: 二维矩阵展成1维 那么做法就是把A矩阵变成一个1*(n*n)的一维向量,然后构造一个(n*n)*(n*n)的辅助矩阵 我们观察到对于A中的每一个元素,每一次卷积运算,所要求乘积的值的位置是

2018 ICPC 徐州网络赛

2018 ICPC 徐州网络赛 A. Hard to prepare 题目描述:\(n\)个数围成一个环,每个数是\(0\)~\(2^k-1\),相邻两个数的同或值不为零,问方案数. solution 将环变成链,设\(f[i][0\)~\(2]\),分别表示与第一个数相同,与第一个数不同,与第一个数相同,与第一个数的反相同.然后\(dp\)即可. 时间复杂度:\(O(n)\) B. BE, GE or NE solution 根据题目描述\(dp\)即可. 时间复杂度:\(O(nm)\) C.