- 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
题目来源
思路:
最大生成树+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