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.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章