首页 > 其他 > 详细

HDU2874(LCA应用:求两点之间距离,图不连通)

时间:2016-02-02 22:24:22      阅读:138      评论:0      收藏:0      [点我收藏+]

Connections between cities

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


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
模板题,注意该题图不连通,在tarjan算法中将d[]设为-1.
RMQ+LCA,在线算法
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAXN=10005;
struct Edge{
    int to,cost,next;
}es[MAXN*2];
int head[MAXN],tot;
void add_edge(int u,int v,int cost)
{
    es[tot].to=v;
    es[tot].cost=cost;
    es[tot].next=head[u];
    head[u]=tot++;
}
int V,E,Q;
int dp[MAXN*2][20];
int depth[MAXN*2];
int vs[MAXN*2];
int id[MAXN];
int cnt,dep;
int d[MAXN];
void dfs(int u,int fa)
{
    int temp=++dep;
    depth[++cnt]=temp;
    vs[temp]=u;
    id[u]=cnt;
    for(int i=head[u];i!=-1;i=es[i].next)
    {
        int to=es[i].to;
        if(to==fa)    continue;
        d[to]=d[u]+es[i].cost;
        dfs(to,u);
        depth[++cnt]=temp;
    }
}

void init_rmq(int n)
{
    for(int i=1;i<=n;i++)    dp[i][0]=depth[i];
    
    int m=floor(log(n*1.0)/log(2.0));
    for(int j=1;j<=m;j++)
        for(int i=1;i<=n-(1<<j)+1;i++)
            dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
int rmq(int a,int b)
{
    int k=floor(log((b-a+1)*1.0)/log(2.0));
    return min(dp[a][k],dp[b-(1<<k)+1][k]);
}
int LCA(int u,int v)
{
    if(id[u]>id[v])    swap(u,v);
    int k=rmq(id[u],id[v]);
    return vs[k];
}

int par[MAXN],rnk[MAXN];
void init_set(int n)
{
    for(int i=0;i<=n;i++)
    {
        par[i]=i;
        rnk[i]=0;
    }
}
int fnd(int x)
{
    if(par[x]==x)
        return x;
    return par[x]=fnd(par[x]);
}
void unite(int x,int y)
{
    int a=fnd(x);
    int b=fnd(y);
    if(a==b)    return ;
    if(rnk[a]<rnk[b])
    {
        par[a]=b;
    }
    else
    {
        par[b]=a;
        if(rnk[a]==rnk[b])    rnk[a]++;
    }
}
bool same(int x,int y)
{
    return fnd(x) == fnd(y);
}
int main()
{
    while(scanf("%d%d%d",&V,&E,&Q)!=EOF)
    {
        tot=0;
        memset(head,-1,sizeof(head));
        cnt=0;
        dep=0;
        memset(d,0,sizeof(d));
        memset(id,0,sizeof(id));
        init_set(V);
        for(int i=0;i<E;i++)
        {
            int u,v,cost;
            scanf("%d%d%d",&u,&v,&cost);
            add_edge(u,v,cost);
            add_edge(v,u,cost);
            unite(u,v);
        }    
        for(int i=1;i<=V;i++)
            if(!id[i])    dfs(i,-1);
        init_rmq(cnt);        
        while(Q--)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            if(same(u,v))
            {
                printf("%d\n",d[u]+d[v]-2*d[LCA(u,v)]);
            }
            else
            {
                printf("Not connected\n");
            }
        }
    }
    return 0;
}

 

HDU2874(LCA应用:求两点之间距离,图不连通)

原文:http://www.cnblogs.com/program-ccc/p/5178571.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!