HDU 2874 Connections between cities

Connections between cities

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9785    Accepted Submission(s): 2357


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

【题意】给了一个深林,求任意两点的距离,如果不连通,输出不连通的信息。

【解题方法】深林转化成树之后就是LCA模板了。方法很多。时间很松的,谁便哪种。

【AC 代码】

//
//Created By just_sort 2016/9/2
//All rights reserved
//HDU.2874 Euler Sequence & RMQ(ST)
//

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e4+10;
const int maxm = 2e4+10;
struct edge{
    int v,w,next;
}E[maxn<<2];
int head[maxn],tot;
void addedge(int u,int v,int w){
    E[tot].v=v,E[tot].w=w,E[tot].next=head[u],head[u]=tot++;
}
void init(){
    memset(head,-1,sizeof(head));
    tot=0;
}
/****************************get lca********************************/
int n,m,q,vis[maxm],dep[maxm],first[maxn],dp[maxn],id[maxn],dfsnum,cc;
int getmin(int x,int y){
    return dep[x]<dep[y]?x:y;
}
struct SparseTable{
    int dp[20][maxn<<1];
    void init(int n){
        for(int i=1; i<=n; i++) dp[0][i]=i;
        for(int i=1; (1<<i)<=n; i++)
            for(int j=1; j+(1<<i)-1<=n; j++)
                dp[i][j]=getmin(dp[i-1][j],dp[i-1][j+(1<<(i-1))]);
    }
    int RMQ(int l,int r){
        int k=31-__builtin_clz(r-l+1);
        return getmin(dp[k][l],dp[k][r-(1<<k)+1]);
    }
}st;
void dfs(int u,int f,int d)
{
    vis[++dfsnum]=u;
    dep[dfsnum]=d;
    first[u]=dfsnum;
    id[u]=cc;
    for(int i=head[u]; ~i; i=E[i].next){
        int v=E[i].v;
        if(v==f) continue;
        dp[v]=dp[u]+E[i].w;
        dfs(v,u,d+1);
        vis[++dfsnum]=u;
        dep[dfsnum]=d;
    }
}
void build(){
    dfsnum=cc=0;
    memset(id,0,sizeof(id));
    for(int i=1; i<=n; i++){
        ++cc;
        if(!id[i]) dp[i]=0,dfs(i,-1,0);
    }
    st.init(2*n-1);
}
int lca(int u,int v)
{
    if(first[u]>first[v]) swap(u,v);
    int id=st.RMQ(first[u],first[v]);
    return vis[id];
}
/*****************************分割线**********************************/

int main()
{
    while(scanf("%d%d%d",&n,&m,&q)!=EOF)
    {
        init();
        int u,v,w;
        while(m--)
        {
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
            addedge(v,u,w);
        }
        build();
        while(q--)
        {
            scanf("%d%d",&u,&v);
            if(id[u]!=id[v]) puts("Not connected");
            else{
                printf("%d\n",dp[u]+dp[v]-2*dp[lca(u,v)]);
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值