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;
}
复制代码

 

posted on   vCoders  阅读(431)  评论(0编辑  收藏  举报

编辑推荐:
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
阅读排行:
· 官方的 MCP C# SDK:csharp-sdk
· 一款 .NET 开源、功能强大的远程连接管理工具,支持 RDP、VNC、SSH 等多种主流协议!
· 提示词工程师自白:我如何用一个技巧解放自己的生产力
· 一文搞懂MCP协议与Function Call的区别
· 如何不购买域名在云服务器上搭建HTTPS服务
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示