标签:man names bbb print HERE void length between cstring
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×M little squares. That is to say, the height of the rectangle is N and the width of the rectangle is M. 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 Q 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 N and M (1≤N,M≤500), giving the number of rows and columns of the maze.
The next N×MN \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,2) ? (1,M) , (2,1) , (2,2), ? , (2,M), ? ,(N,M).
Each line contains two characters D and R and two integers aaa , b (0≤a,b≤20000000000), aaa is the cost of building the wall between it and its lower adjacent square, and bbb 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 000.
The next line contains an integer Q (1000001≤Q≤100000 ), which represents the number of questions.
The next Q lines gives four integers, x1, y1, x2, y2 ( 1≤x1? , x2≤N , 1≤y1,y1? , y2≤M ), which represent two squares and their coordinate are (x1 , y1) and (x2 , y2).
(x,y) means row xxx and column yyy.
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 <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <vector> #include <queue> #include <stack> #include <cstdlib> #include <iomanip> #include <cmath> #include <cassert> #include <ctime> #include <map> #include <set> using namespace std; #pragma comment(linker, "/stck:1024000000,1024000000") #pragma GCC diagnostic error "-std=c++11" #define lowbit(x) (x&(-x)) #define max(x,y) (x>=y?x:y) #define min(x,y) (x<=y?x:y) #define MAX 100000000000000000 #define MOD 1000000007 #define esp 1e-9 #define pi acos(-1.0) #define ei exp(1) #define PI 3.1415926535897932384626433832 #define ios() ios::sync_with_stdio(true) #define INF 0x3f3f3f3f typedef long long ll; int n,m,f[250006],k=0; int vis[250006],dis[250006]; int ans[100005]; int fa[250006]; struct node { int u,v; ll w; bool operator<(const node &a) const { return a.w<w; } }e[600006]; struct N{ int v,d; N(int x,int y){ v=x; d=y; } }; vector<int>vec[250006]; vector<N>que[250006]; void add(int u,int v,int w) { e[k].u=u;e[k].v=v;e[k++].w=w; } int find(int x) { return f[x]=(f[x]==x?x:find(f[x])); } int kruskal() { int sum=0;int o=n*m; for(int i=0;i<k;i++) { int u=find(e[i].u); int v=find(e[i].v); if(u!=v) { sum+=e[i].w; if(u<v) f[u]=v; else f[v]=u; vec[e[i].u].push_back(e[i].v); vec[e[i].v].push_back(e[i].u); if(--o==1) break; } } } int find_fa(int x){ return fa[x]=(fa[x]==x?x:find_fa(fa[x])); } void dfs(int u,int father) { fa[u]=u; vis[u]=1; for(int i=0;i<vec[u].size();i++) { if(vec[u][i]==father) continue; else { dis[vec[u][i]]=dis[u]+1; dfs(vec[u][i],u); } } for(int i=0;i<que[u].size();i++) { int p=que[u][i].v; if(vis[p]) { int pp=find_fa(p); ans[que[u][i].d]=abs(dis[u]-dis[pp])+abs(dis[p]-dis[pp]); } } fa[u]=father; } int main() { scanf("%d%d",&n,&m); int l=n*m; for(int i=0;i<=l;i++) f[i]=i; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { char a,b; ll x,y; cin>>a>>x>>b>>y; int u=(i-1)*m+j,v; if(i<n){ v=i*m+j; add(u,v,x); } if(j<m){ v=(i-1)*m+j+1; add(u,v,y); } } } sort(e,e+k); kruskal(); int q; scanf("%d",&q); for(int i=1;i<=q;i++) { int x,y,c,d; scanf("%d%d%d%d",&x,&y,&c,&d); int u=(x-1)*m+y; int v=(c-1)*m+d; que[u].push_back(N(v,i)); que[v].push_back(N(u,i)); } dfs(1,0); for(int i=1;i<=q;i++) printf("%d\n",ans[i]); return 0; }
ACM-ICPC 2018 徐州赛区网络预赛 J Maze Designer
标签:man names bbb print HERE void length between cstring
原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/9614266.html