hdu2874 lca问题的Tarjan算法 内存险过

本文探讨了在战后城市重建中如何解决材料运输路径问题。通过构建无圈且可能不连通的城市间道路网络模型,使用特定算法寻找任意两城市间的最短路径或判断是否连通。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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

换了个姿势还是上了HDU     据学长给我的图论题库来说    lca问题也写完了

这题几乎还是模板题       只不过是多了个  判断是否在同一棵树上      用了点小技巧    再Tarjan时  把根节点也传上去    如果得结果时根节点不同就跳过

另外这题灰常容易爆内存     稍微改了一下用 C++水过去了   然而G++还是爆………………

ac code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>

using namespace std;
const int maxn=10005;
const int inf=0x3f3f3f3f;

struct node
{
    int v,d;
    node* nxt;
};

node *head1[maxn],edge1[maxn*2];
node *head2[maxn],edge2[2000002];
int root[maxn],ans[1000002],dis[maxn];
int vis[maxn];
int n,lnum,qnum,idx1,idx2;

void init()
{
    for(int i=0;i<=n;i++)
    {
        head1[i]=head2[i]=0;
        vis[i]=0;
    }
    idx1=idx2=0;
}

int fin(int x)
{
    if(root[x]==x) return x;
    return root[x]=fin(root[x]);
}

void add(int u,int v,int d,node *head[],node edge[],int& idx)
{
    edge[idx].v=v;
    edge[idx].d=d;
    edge[idx].nxt=head[u];
    head[u]=edge+idx++;

    edge[idx].v=u;
    edge[idx].d=d;
    edge[idx].nxt=head[v];
    head[v]=edge+idx++;
}

void Tarjan(int u,int rot)
{
    root[u]=u;
    vis[u]=rot;
    for(node* p=head2[u];p;p=p->nxt)
        if(vis[p->v]==rot)
            ans[p->d]=dis[u]+dis[p->v]-2*dis[fin(p->v)];
    for(node* p=head1[u];p;p=p->nxt)
        if(!vis[p->v])
        {
            dis[p->v]=dis[u]+p->d;
            Tarjan(p->v,rot);
            root[p->v]=u;
        }
}

int main()
{
    int u,v,d;
    while(scanf("%d%d%d",&n,&lnum,&qnum)!=EOF)
    {
        init();
        for(int i=1;i<=lnum;i++)
        {
            scanf("%d %d %d",&u,&v,&d);
            add(u,v,d,head1,edge1,idx1);
        }
        for(int i=1;i<=qnum;i++)
        {
            scanf("%d %d",&u,&v);
            ans[i]=inf;
            add(u,v,i,head2,edge2,idx2);
        }
        for(int i=1;i<=n;i++)
            if(!vis[i]) 
            {
                dis[i]=0;
                Tarjan(i,i);
            }
        for(int i=1;i<=qnum;i++)
        {
            if(ans[i]==inf) puts("Not connected");
            else printf("%d\n",ans[i]);
        }
    }
    return 0;
}
















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值