Connections between cities

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

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

Hint

Hint

Huge input, scanf recommended.

 
Source
 
Recommend
gaojie   |   We have carefully selected several similar problems for you:  2873 2876 2872 2875 2877 
 
 
题目重现:

第十次世界大战以后,很多城市受到严重破坏,我们需要重建这些城市。 然而,所需的一些材料只能在某些地方生产。 所以我们需要将这些材料从城市运送到城市。 大多数道路在战争期间已经完全被摧毁,两个城市之间可能没有道路,也没有圈子。
现在,你的任务来了。 在给你道路的条件之后,我们想知道是否存在任何两个城市之间的路径。 如果答案是肯定的,输出它们之间的最短路径。

思路:

看到这个题,是不是又激动半天,天哪,这不又是个模板吗?!

不过,这个题比起以前的几个题来要高级那么一点点、、、这个题是森林,上几个题是棵树。

所以我们这个题进行处理的时候我们要判断两个点是否连通,也就是说他们是否在一棵树里。

我们用并查集处理就好了,剩下的就是lca的模板了、、、

代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 21000
using namespace std;
bool vis[N];
int t,n,m,x,y,z,fx,fy,ans,tot;
int fa[N],dad[N],top[N],size[N],deep[N],head[N],dis[N];
int find(int x)
{
    if(x==dad[x]) return x;
    dad[x]=find(dad[x]);
    return dad[x];
}
int read()
{
    ,f=; char ch=getchar();
    ; ch=getchar();}
    +ch-'; ch=getchar();}
    return x*f;
}
struct Edge
{
    int to,dis,next,from;
}edge[N<<];
int add(int x,int y,int z)
{
    tot++;
    edge[tot].to=y;
    edge[tot].dis=z;
    edge[tot].next=head[x];
    head[x]=tot;
}
int begin()
{
    ans=;tot=;
    memset(fa,,sizeof(fa));
    memset(top,,sizeof(top));
    memset(dis,,sizeof(dis));
    memset(vis,,sizeof(vis));
    memset(edge,,sizeof(edge));
    memset(deep,,sizeof(deep));
    memset(size,,sizeof(size));
    memset(head,,sizeof(head));
}
int lca(int x,int y)
{
    for(;top[x]!=top[y];x=fa[top[x]])
     if(deep[top[x]]<deep[top[y]]) swap(x,y);
    if(deep[x]>deep[y]) swap(x,y);
    return x;
}
int dfs(int x)
{
    vis[x]=;
    deep[x]=deep[fa[x]]+;
    for(int i=head[x];i;i=edge[i].next)
    {
        int to=edge[i].to;
        if(fa[x]==to) continue;
        dis[to]=dis[x]+edge[i].dis;
        fa[to]=x;dfs(to);
        size[x]+=size[to];
    }
}
int dfs1(int x)
{
    ;
    if(!top[x]) top[x]=x;
    for(int i=head[x];i;i=edge[i].next)
    {
        int to=edge[i].to;
        if(fa[x]!=to&&size[t]<size[to]) t=to;
    }
    if(t) top[t]=top[x],dfs1(t);
    for(int i=head[x];i;i=edge[i].next)
    {
        int to=edge[i].to;
        if(fa[x]!=to&&to!=t) dfs1(to);
    }
}
int pd(int x,int y)
{
    if(find(x)==find(y)) return false;
    return true;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        m=read(),t=read();begin();
        ;i<=n;i++) dad[i]=i;
        ;i<=m;i++)
        {
            x=read(),y=read(),z=read();
            add(x,y,z),add(y,x,z);
            fx=find(x),fy=find(y);
            if(fx!=fy)  dad[fx]=fy;
        }
        ;i<=n;i++)
          if(!vis[i]) dfs(i),dfs1(i);
        ;i<=t;i++)
        {
            x=read(),y=read();
            if(pd(x,y)) printf("Not connected\n");
            else
            {
                ans=dis[x]+dis[y]-*dis[lca(x,y)];
                printf("%d\n",ans);
            }
        }
    }
    ;
}

HDU——2874 Connections between cities的更多相关文章

  1. hdu 2874 Connections between cities [LCA] (lca->rmq)

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  2. HDU 2874 Connections between cities(LCA Tarjan)

    Connections between cities [题目链接]Connections between cities [题目类型]LCA Tarjan &题意: 输入一个森林,总节点不超过N ...

  3. hdu 2874 Connections between cities 带权lca判是否联通

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  4. hdu 2874 Connections between cities(st&rmq LCA)

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  5. hdu 2874 Connections between cities (并查集+LCA)

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  6. HDU 2874 Connections between cities(LCA)

    题目链接 Connections between cities LCA的模板题啦. #include <bits/stdc++.h> using namespace std; #defin ...

  7. HDU 2874 Connections between cities (LCA)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 题意是给你n个点,m条边(无向),q个询问.接下来m行,每行两个点一个边权,而且这个图不能有环路 ...

  8. HDU 2874 Connections between cities(LCA离线算法实现)

    http://acm.hdu.edu.cn/showproblem.php?pid=2874 题意: 求两个城市之间的距离. 思路: LCA题,注意原图可能不连通. 如果不了解离线算法的话,可以看我之 ...

  9. HDU 2874 Connections between cities(LCA(离线、在线)求树上距离+森林)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 题目大意:给出n个点,m条边,q个询问,每次询问(u,v)的最短距离,若(u,v)不连通即不在同 ...

随机推荐

  1. springmvc 获取数组

    spingmvc 获取数据有这几种方式:1.通过HttpRequestServlet的方法获取数据.2.form表单传递对象字段,springmvc自动获取.3.ajax 请求通过注解的方式直接获取数 ...

  2. Fragment中获取Activity的Context (转)

    Fragment中获取Activity的Context时只需要this.getActivity()即可.     而不是许多人说的this.getActivity().getApplicationCo ...

  3. <jsp:include page="${pageContext.request.contextPath/index.jsp" ></jsp:include> 引发的错误

    路径引发的错误 如下使用项目路径对jsp页面进行获取,会报javax.servlet.ServletException: File "/web/dbwx/web/public/page_to ...

  4. struts2 使用json

    前台代码: Struts.xml: UserAction: 注意: 1)struts类库里面没有提供ezmorph-1.0.6.jar文件,所以要手动添加:

  5. MySQL 中去重 distinct 用法

    在使用MySQL时,有时需要查询出某个字段不重复的记录,这时可以使用mysql提供的distinct这个关键字来过滤重复的记录,但是实际中我们往往用distinct来返回不重复字段的条数(count( ...

  6. 面试之JVM

    目录 Java虚拟机 类的加载方式 Java内存模型 程序计数器(PC) Java虚拟机栈(Stack) 本地方法栈 元空间(MetaSpace) Java堆(Heap) 内存分配策略 垃圾回收(GC ...

  7. LAME的“命令行”

    VBR 编码 (强烈推荐) Alt Preset Extreme (平均256kbps) 我们有时在网上可以看到".LAME-APX." 就是这种形式,我们也可以在文件名中包含这个 ...

  8. NOIP专题复习3 图论-强连通分量

    目录 一.知识概述 二.典型例题 1.[HAOI2006]受欢迎的牛 2.校园网络[[USACO]Network of Schools加强版] 三.算法分析 (一)Tarjan算法 (二)解决问题 四 ...

  9. codevs1288 埃及分数

    题目描述: 在古埃及,人们使用单位分数的和(形如1/a的, a是自然数)表示一切有理数. 如:2/3=1/2+1/6,但不允许2/3=1/3+1/3,因为加数中有相同的. 对于一个分数a/b,表示方法 ...

  10. 使用枚举(emum)代替常量类

    原文: 作者:逍遥不羁 来源:CSDN 原文:https://blog.csdn.net/javaloveiphone/article/details/52371706 版权声明:本文为博主原创文章, ...