HDU 2874 LCA在线算法RMQ



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.
 

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 2 1 3 2 2 4 3 5 2 3 1 4 4 5
 

Sample Output
      
      
Not connected 6
Hint
Hint Huge 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.
 

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 2 1 3 2 2 4 3 5 2 3 1 4 4 5
 

Sample Output
       
       
Not connected 6
Hint
Hint Huge input, scanf recommended.
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值