ZOJ 3195 Design the city LCA转RMQ在线
来源:互联网 发布:本子软件怎么用 编辑:程序博客网 时间:2024/05/29 05:55
Cerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terrible, that there are traffic jams everywhere. Now, Cerror finds out that the main reason of them is the poor design of the roads distribution, and he want to change this situation.
In order to achieve this project, he divide the city up to N regions which can be viewed as separate points. He thinks that the best design is the one that connect all region with shortest road, and he is asking you to check some of his designs.
Now, he gives you an acyclic graph representing his road design, you need to find out the shortest path to connect some group of three regions.
Input
The input contains multiple test cases! In each case, the first line contian a interger N (1 < N < 50000), indicating the number of regions, which are indexed from 0 to N-1. In each of the following N-1 lines, there are three interger Ai, Bi, Li (1 < Li < 100) indicating there's a road with length Li between region Ai and region Bi. Then an interger Q (1 < Q < 70000), the number of group of regions you need to check. Then in each of the following Q lines, there are three interger Xi, Yi, Zi, indicating the indices of the three regions to be checked.
Process to the end of file.
Output
Q lines for each test case. In each line output an interger indicating the minimum length of path to connect the three regions.
Output a blank line between each test cases.
Sample Input
40 1 10 2 10 3 121 2 30 1 250 1 10 2 11 3 11 4 120 1 21 0 3
Sample Output
3222
1,参考一个不错的博客:http://www.cnblogs.com/scau20110726/archive/2013/05/26/3100812.html
2,这题离线的tarjan不好做,所以用在线的。
3,大致就是dfs跑出序列,然后RMQ直接找到祖先。
///ZOJ 3195 LCA在线做法///LCA转RMQ 应用深度搜索,把树变成序列,按照两个点第一次出现的位置,给出范围///找范围最小深度的位置,从而找到最近公共祖先#include<iostream>using namespace std;const int maxn = 100005 ;struct Edge { int form,to,dist;};vector<Edge> edges ;vector<int>G[maxn] ;bool vis[maxn] ;///标记数组int len[maxn] ;///节点到根的长度int ver[maxn<<1] ;///遍历的节点序列int first[maxn] ;///第一次出现的位置int R[maxn<<1] ;///遍历的深度int d[maxn][50] ;///dp数组,里面放的是RMQ的位置,不是数值int n,tot ;void add_edge(int from,int to,int dist) { edges.push_back((Edge) {from,to,dist}) ; int mm = edges.size() ; G[from].push_back(mm-1) ;}void dfs(int u,int dept) { vis[u] = true ; ver[tot] = u ; R[tot] = dept ; first[u] = tot ; tot++ ; for(int i=0; i<G[u].size(); i++) { if(!vis[edges[G[u][i]].to]) { len[edges[G[u][i]].to] = len[u]+ edges[G[u][i]].dist ; dfs(edges[G[u][i]].to,dept+1) ; ver[tot] = u ; R[tot] = dept ; tot++ ; } }}void RMQ_init(){ for(int i=0;i<tot;i++){///初始化,存的是位置 d[i][0] = i ; } for(int j=1;(1<<j)<=tot;j++){ for(int i=0;i+(1<<j)-1<tot;i++){///取最深度最小时的位置 int a = d[i][j-1] ; int b = d[i+(1<<(j-1))][j-1] ; if(R[a]<R[b])d[i][j] = d[i][j-1] ; else d[i][j] = d[i+(1<<(j-1))][j-1] ; } }}int RMQ(int l,int r){///返回范围最小深度的位置 if(l>r)return 0; int k=0; while((1<<(k+1)) <= r-l+1) k++; int a = d[l][k] ; int b = d[r-(1<<k)+1][k] ; if(R[a]<R[b])return a ; else return b ;}int LCA(int u ,int v) ///返回点u和点v的LCA{ int x = first[u] , y = first[v]; if(x > y) swap(x,y); int res = RMQ(x,y); return ver[res];}int main() { int x=0; while(~scanf("%d",&n)) { if(x==1)printf("\n") ; x=1; memset(vis,false,sizeof(vis)) ; memset(d,false,sizeof(d)) ; memset(G,false,sizeof(G)) ; edges.clear() ; for(int i=0; i<n-1; i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w) ; add_edge(u,v,w) ; add_edge(v,u,w) ; } tot = 0; dfs(0,1) ; RMQ_init() ; int q ; scanf("%d",&q) ; for(int i=0; i<q; i++) { int a,b,c ; scanf("%d%d%d",&a,&b,&c) ; int ans = 0 ; ///和两个点的时候有点区别 ans+=len[a]+len[b]+len[c]-(len[LCA(a,b)]+len[LCA(a,c)]+len[LCA(b,c)]); printf("%d\n",ans) ; } } return 0;}
- ZOJ 3195 Design the city LCA转RMQ在线
- ZOJ 3195 Design the city LCA转RMQ
- zoj 3195 Design the city 【LCA转RMQ】 【求三点最短距离】
- ZOJ 题目3195 Design the city(LCA+RMQ)
- ZOJ 3195 Design the city (在线LCA,4级)
- zoj 3195 Design the city LCA
- zoj 3195 Design the city (LCA)
- Design the city - ZOJ 3195 LCA
- zoj 3195 Design the city(LCA)
- ZOJ - 3195 Design the city(LCA)
- ZOJ 3195Design the city LCA
- ZOJ Problem Set - 3195 Design the city 【Tarjan离线LCA】
- ZOJ 3195 - Design the city(LCA'离线算法Tarjan)
- ZOJ 3195 Design the city(LCA Tarjan)
- ZOJ 3195 Design the city(LCA变形)
- 3195 Design the city //LCA
- Zoj 3195 Design the city
- ZOJ 3195 Design the city
- HTTP请求方式GET和POST的区别
- cuda环境下安装opencv出现nvcc warning : The 'compute_11'
- Mocha测试接口类型
- struct和typedef struct的关联及区别
- Unity Shader之Tessellation
- ZOJ 3195 Design the city LCA转RMQ在线
- leetcode 454. 4Sum II
- (SpringMVC)springmvc不可以用成员变量的map
- vue2 前后端分离项目ajax跨域session问题解决
- Java 计算对象的长度
- oracle语句报错 * ERROR at line 2: ORA-00923: FROM keyword not found where expected
- 单入口文件时候不能把引用多次的模块打印到commonChunkPlugin中
- Tomcat中Context标签使用
- js中i++和++i的区别