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

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;
}

 

#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
const int MAXN=10001;
struct Edge{
    int to,cost,next;
}es[2*MAXN];
int V,E,Q;
struct Query{
    int v,next;
}qs[200*MAXN];
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 qhead[MAXN],ant;
void add_query(int u,int v)
{
    qs[ant].v=v;
    qs[ant].next=qhead[u];
    qhead[u]=ant++;
}

int d[MAXN];
int ans[200*MAXN];
int par[MAXN];
bool vis[MAXN];
int fnd(int x)
{
    if(x==par[x])
        return x;
    return par[x]=fnd(par[x]);
}
void dfs(int u,int fa)
{    
    par[u]=u;
    vis[u]=true;
    for(int i=head[u];i!=-1;i=es[i].next)
    {
        int v=es[i].to;
        if(vis[v])    continue;
        d[v]=d[u]+es[i].cost;
        dfs(v,u);
        par[v]=u;
    }
    for(int i=qhead[u];i!=-1;i=qs[i].next)
    {
        int v=qs[i].v;
        if(vis[v])
        {
            if(d[v]!=-1)
                ans[i/2]=d[u]+d[v]-2*d[fnd(v)];
            else
                ans[i/2]=-1;
        }
    }
}
int main()
{
    while(~scanf("%d %d %d",&V,&E,&Q))
    {
        tot=0;
        memset(head,-1,sizeof(head));
        ant=0;
        memset(qhead,-1,sizeof(qhead));
        memset(vis,false,sizeof(vis));
        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);
        }
        for(int i=0;i<Q;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            add_query(u,v);
            add_query(v,u);
        }
        for(int i=1;i<=V;i++)
            if(!vis[i])
            {
                memset(d,-1,sizeof(d));
                d[i]=0;
                dfs(i,-1);
            }
        for(int i=0;i<Q;i++)
            if(ans[i]==-1)    printf("Not connected\n");
            else printf("%d\n",ans[i]);    
    }
    return 0;
}

 

你可能感兴趣的:(HDU2874(LCA应用:求两点之间距离,图不连通))