hdu2874Connections between cities

本文探讨了在战后重建过程中如何有效运输材料的城市间路径寻找算法。通过给出城市间的道路条件,算法能够判断任意两座城市间是否存在路径,并计算出最短路径长度。采用特定的数据结构和算法技巧,确保在大规模数据集上也能高效运行。

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

Connections between cities

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


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
思路:如果有公共祖先就直接用深度差来求,如果没有公共祖先输出“Not connected”。
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
using namespace std;
const int inf=0x3f3f3f3f;
const int M=2000005;
const int N=10005;
int head[N],h[N],dis[N],f[N],a[M/2],vis[N];
int cnt,num,tt;
int n,m,z;
struct node
{
    int v,w,next;
}e[N<<1],q[M];
void addedge(int u,int v,int w)
{
    e[num].v=v;
    e[num].w=w;
    e[num].next=head[u];
    head[u]=num++;
    e[num].v=u;
    e[num].w=w;
    e[num].next=head[v];
    head[v]=num++;
}
void addque(int u,int v,int w)
{
     q[tt].v=v;
     q[tt].w=w;
     q[tt].next=h[u];
    h[u]=tt++;
    q[tt].v=u;
     q[tt].w=w;
     q[tt].next=h[v];
    h[v]=tt++;
}
void init()
{
    memset(head,-1,sizeof(head));
    memset(h,-1,sizeof(h));
    memset(vis,-1,sizeof(vis));
    memset(f,-1,sizeof(f));
    memset(dis,0,sizeof(dis));
    memset(a,-1,sizeof(a));
    cnt=num=tt=0;
}
int findx(int x)
{
    if(x==f[x])return x;
    return f[x]=findx(f[x]);
}
void tarjan(int u,int w,int r)
{
    dis[u]=w;
    f[u]=u;
    vis[u]=r;
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].v;
        if(vis[v]==-1)
        {
            tarjan(v,w+e[i].w,r);
            f[v]=u;
        }
    }
    for(int i=h[u];i!=-1;i=q[i].next)
    {
        int v=q[i].v;
        if(vis[v]==r)
        {
            a[q[i].w]=dis[v]+dis[u]-2*dis[findx(v)];
        }
    }
}
int main()
{
    while(~scanf("%d%d%d",&n,&m,&z))
    {
        init();
        int i;
        int u,v,w;
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);

        }
        for(i=0;i<z;i++)
        {
            scanf("%d%d",&u,&v);
            addque(u,v,i);
        }
        for(i=1;i<=n;i++)
        {
            if(vis[i]==-1)
            {
                tarjan(i,0,i);
            }
        }
        for(i=0;i<z;i++)
        {
            if(a[i]!=-1)
            printf("%d\n",a[i]);
            else printf("Not connected\n");
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值