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. AJPFX总结面向对象特征之一的继承知识

    继 承(面向对象特征之一) 好处: 1:提高了代码的复用性. 2:让类与类之间产生了关系,提供了另一个特征多态的前提.   父类的由来:其实是由多个类不断向上抽取共性内容而来的. java中对于继承, ...

  2. actuator服务实战

    1. actuator服务实战 1.1. 前言 actuator默认集成了很多端点查看,这里我会挑选也用到可能性大些的 1.2. Endpoints 1.2.1. 使用方式 开启服务后,直接访问:lo ...

  3. thinkphp查询,3.X 5.0 亲试可行

    [php] view plain copy   print? 一.介绍 ThinkPHP内置了非常灵活的查询方法,可以快速的进行数据查询操作,查询条件可以用于读取.更新和删除等操作,主要涉及到wher ...

  4. addslashes,stripslashes

    官方介绍: (PHP 4, PHP 5) addslashes — 使用反斜线引用字符串 返回字符串,该字符串为了数据库查询语句等的需要在某些字符前加上了反斜线.这些字符是单引号(’).双引号(”). ...

  5. 迅为i.MX6UL核心板ARMCortex-A7单核NXP飞思卡尔工控行业Imx6核心板

    iMX6UL核心板小巧精致,尺寸仅38mm*42mm:CPU型号iMX6UL@ 528MHz ARM Cortex-A7架构 :内存:512M DDR :存储:8G EMMC,低功耗,性能强大,性价比 ...

  6. Dynamic type checking and runtime type information

    动态类型的关键是将动态对象与实际类型信息绑定. See also: Dynamic programming language and Interpreted language Dynamic type ...

  7. 获取windows版本号

    原文:https://blog.csdn.net/justFWD/article/details/44856277 内容整理如下,点击跳至指定内容: manifest文件加上compatibility ...

  8. IDEA -- idea无法导入HttpServlet包解决方法

    IntelliJ IDEA 没有导入 servlet-api.jar 这个架包,需要你手动导入支持. 步骤1: 步骤2: 步骤3: 在弹出框中找到Tomcat安装路径 下的lib文件夹..中的Serv ...

  9. nginx配置错误页面

    有时候页面会遇到404页面找不到错误,或者是500.502这种服务端错误,这时候我们可能希望自己定制返回页面,不希望看到默认的或者是内部的错误页面,可以通过nginx配置来实现. 1 50x错误对于5 ...

  10. Go:struct

    一.使用方式 方式3和方式4返回的是:结构体指针,编译器底层对 p.Name 做了转化 *(p).Name,方便程序员使用. type Person struct { Name string Age ...