HDU 2874 LCA在线算法RMQ
来源:互联网 发布:tomcat修改1099端口 编辑:程序博客网 时间:2024/06/07 05:14
Connections between cities
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11256 Accepted Submission(s): 2628
Problem Description
After World War X, a lot of cities have been seriously damaged, and we need to rebuild those cities. However, some materials needed can only be produced in certain places. So we need to transport these materials from city to city. For most of roads had been totally destroyed during the war, there might be no path between two cities, no circle exists as well.
Now, your task comes. After giving you the condition of the roads, we want to know if there exists a path between any two cities. If the answer is yes, output the shortest path between them.
Now, your task comes. After giving you the condition of the roads, we want to know if there exists a path between any two cities. If the answer is yes, output the shortest path between them.
Input
Input consists of multiple problem instances.For each instance, first line contains three integers n, m and c, 2<=n<=10000, 0<=m<10000, 1<=c<=1000000. n represents the number of cities numbered from 1 to n. Following m lines, each line has three integers i, j and k, represent a road between city i and city j, with length k. Last c lines, two integers i, j each line, indicates a query of city i and city j.
Output
For each problem instance, one line for each query. If no path between two cities, output “Not connected”, otherwise output the length of the shortest path between them.
Sample Input
5 3 21 3 22 4 35 2 31 44 5
Sample Output
Not connected6HintHintHuge input, scanf recommended.
解题思路
1,离线的trajan不能做,要查询的太多,都存下来离线处理的话就内存炸了
2,然后就是LCA转RMQ的模板题了,因为要判断不联通,并查集搞一下。
#include<bits/stdc++.h>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 f[maxn];int n,tot ;int find(int x){ return x==f[x]?x:f[x]=find(f[x]) ;}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) { f[u] = u ; 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) ; f[edges[G[u][i]].to] = find(u) ; 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 m,c; while(~scanf("%d%d%d",&n,&m,&c)) { memset(vis,false,sizeof(vis)) ; memset(d,false,sizeof(d)) ; memset(G,false,sizeof(G)) ; edges.clear() ; for(int i=0; i<m; i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w) ; u--;v--; add_edge(u,v,w) ; add_edge(v,u,w) ; } tot = 0; for(int i=0;i<n;i++){ if(!vis[i]){ dfs(i,1) ; } } RMQ_init() ; for(int i=0; i<c; i++) { int a,b; scanf("%d%d",&a,&b) ; a--;b--; if(find(a)!=find(b)){ printf("Not connected\n"); continue ; } int ans=len[a]+len[b]-2*len[LCA(a,b)]; printf("%d\n",ans) ; } } return 0;}
Connections between cities
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11256 Accepted Submission(s): 2628
Problem Description
After World War X, a lot of cities have been seriously damaged, and we need to rebuild those cities. However, some materials needed can only be produced in certain places. So we need to transport these materials from city to city. For most of roads had been totally destroyed during the war, there might be no path between two cities, no circle exists as well.
Now, your task comes. After giving you the condition of the roads, we want to know if there exists a path between any two cities. If the answer is yes, output the shortest path between them.
Now, your task comes. After giving you the condition of the roads, we want to know if there exists a path between any two cities. If the answer is yes, output the shortest path between them.
Input
Input consists of multiple problem instances.For each instance, first line contains three integers n, m and c, 2<=n<=10000, 0<=m<10000, 1<=c<=1000000. n represents the number of cities numbered from 1 to n. Following m lines, each line has three integers i, j and k, represent a road between city i and city j, with length k. Last c lines, two integers i, j each line, indicates a query of city i and city j.
Output
For each problem instance, one line for each query. If no path between two cities, output “Not connected”, otherwise output the length of the shortest path between them.
Sample Input
5 3 21 3 22 4 35 2 31 44 5
Sample Output
Not connected6HintHintHuge input, scanf recommended.
阅读全文
0 0
- HDU 2874 LCA在线算法RMQ
- hdu 2586在线LCA RMQ
- RMQ和LCA在线算法
- LCA转RMQ 在线算法模板
- hdu 2586 LCA在线算法
- RMQ在线求LCA
- LCA在线算法(RMQ st算法的结合)
- LCA三种算法学习(离线算法tarjan+在线算法转rmq+在线倍增)例题poj1330、1470;hdu4547、2874
- HDU 2874 Connections between cities (LCA离线&&在线RMQ,4级)
- hdu 2586 How far away LCA在线RMQ(模板题)
- TOJ 1607 Distance Queries -- LCA在线算法 RMQ
- hdu2586(LCA的一个例子,在线算法RMQ)
- POJ 1986 - Distance Queries(LCA‘ 在线算法RMQ)
- poj1986 LCA转化为RMQ在线算法模板题
- LCA的在线算法, DFS编号 + ST来rmq。
- poj 1330 Nearest Common Ancestors lca rmq在线算法
- ST(RMQ)算法(在线)求LCA
- hdu 2586 lca在线算法(朴素算法)
- 图片无缝向下滚动
- Android应用开发工程师技术栈
- javascript学习(一)— 比较运算与strict模式
- poll_server
- SpringMVC多视图解析器(jsp,html,title解析器)
- HDU 2874 LCA在线算法RMQ
- caffe(一): 利用自带MNIST例程训练手写字符识别模型
- epoll服务器---I/O多路转接之epoll
- JSON.parse()和JSON.stringify()
- linux ./config --prefix=
- LeetCode2:Add Two Numbers
- Pandas Apply函数
- ubuntu linux基本命令- 改变工作目录命令
- 开通新博客了