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. 让px单位自动转换为rem的方法

    开发工具:      编辑器:vscode;     css预处理器:less;(无具体要求): 步骤:   1. vscode安装cssrem插件:   2. 修改css插件的默认配置,其默认转换p ...

  2. React全家桶之一 react-router之高级

    使用query获取URL中的参数 //引入相关的依赖 const Page = props => <div> <h1>{props.location.query.mess ...

  3. Android开发-下载网络图片并显示到本地

    Android下载网络图片的流程是: 发送网络请求->将图片以流的形式下载下来->将流转换为Bitmap并赋给ImageView控件. 注意点 最新的Android系统不可以在主线程上请求 ...

  4. 在Bootstrap中得模态框(modal)中下拉不能显示得问题

    $.fn.modal.Constructor.prototype.enforceFocus = function () { $("#insertModal").on("s ...

  5. java JDK在windows及mac下安装配置

    windows下安装: JDK下载 地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151. ...

  6. 黑马程序员----java基础:多线程

    ------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- ------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ---- ...

  7. 对称加密DES加密

    DES加密: des是对称加密,加密和解密需要相同的秘钥,它的密码最长56位,必须是8的倍数,秘钥越长,越安全. package com.trm.util.encrypt; import java.s ...

  8. JavaScript:JSON 和 JS 对象

    区别 JSON(JavaScript Object Notation)仅仅是一种数据格式(或者叫数据形式).数据格式其实就是一种规范,按照这种规范来存诸和交换数据.就好像 XML 格式一样. 区别 J ...

  9. Android中notifyDataSetInvalidated()和notifyDataSetChanged()有什么区别

     看下源码中对于这两个方法   public void notifyDataSetChanged () 该方法内部实现了在每个观察者上面调用onChanged事件.每当发现数据集有改变的情况,或者读取 ...

  10. Javascript中的For循环

    在开发的过程中,遍历是一个经常遇到的.而for循环则是Javascript工具箱里一个好用的,也常用的工具.每个人的习惯不同,for循环的写法也不尽相同. 1.不写声明变量的写法: for(var i ...