Connections between cities

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

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 

题目大意:

输入n个节点,m条边,q个询问

接着输入i j k,表示i和j城市相连,路长为k

如果两个城市不能到达则输出Not connected,否则输出两个城市之间的距离

题解:

最近公共祖先+并查集

关键是需要把那些分开的树建立起关联,

所以弄个虚拟的0节点,把这些树的根都连在一起

#include<iostream>
#include<vector>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring> using namespace std;
const int N=;
const int M=;
int tot,cnt,n,m,q,s,t;
int head[N],team[N];
int ver[*N]; //ver:保存遍历的节点序列,长度为2n-1,从下标1开始保存
int R[*N]; // R:和遍历序列对应的节点深度数组,长度为2n-1,从下标1开始保存
int first[N]; //first:每个节点在遍历序列中第一次出现的位置
int dir[N]; //dir:保存每个点到树根的距离,很多问题中树边都有权值,会询问两点间的距离,如果树边没权值,相当于权值为1
int dp[*N][M]; struct edge
{
int u,v,w,next;
}e[*N]; void dfs(int u ,int fa,int dep)
{
ver[++tot]=u;
R[tot] = dep;
first[u]=tot;
for(int k=head[u]; k!=-; k=e[k].next)
{
int v=e[k].v, w=e[k].w;
if (v==fa) continue;
dir[v]=dir[u]+w;
dfs(v,u,dep+);
ver[++tot]=u;
R[tot]=dep;
}
} void ST(int n)
{
for(int i=; i<=n; i++) dp[i][] = i;
for(int j=; (<<j)<=n; j++)
{
for(int i=; i+(<<j)-<=n; i++)
{
int a = dp[i][j-], b = dp[i+(<<(j-))][j-];
dp[i][j] = R[a]<R[b]?a:b;
}
}
}
int RMQ(int l,int r)
{
int k=(int)(log((double)(r-l+))/log(2.0));
int a = dp[l][k], b = dp[r-(<<k)+][k]; //保存的是编号
return R[a]<R[b]?a:b;
}
int LCA(int u ,int v)
{
int x = first[u] , y = first[v];
if(x > y) swap(x,y);
int res = RMQ(x,y);
return ver[res];
}
void addedge(int u,int v,int w)
{
e[++cnt].u=u;
e[cnt].v=v;
e[cnt].w=w;
e[cnt].next=head[u];
head[u]=cnt;
}
int findteam(int k)
{
if (team[k]==k) return k;
else return team[k]=findteam(team[k]);
}
int main()
{ while(~scanf("%d%d%d",&n,&m,&q))
{
memset(head,-,sizeof(head));
for(int i=;i<=n;i++) team[i]=i; //并查集
tot=; cnt=;
for(int i=;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
addedge(x,y,z);
addedge(y,x,z);
int fx=findteam(x);
int fy=findteam(y);
if (fx!=fy) team[fy]=fx;
}
for(int i=;i<=n;i++)
if (team[i]==i) //最关键的是给那些分开的树连一个0节点,那样就变成了一棵树
{
addedge(i,,);
addedge(,i,);
}
dir[]=;
dfs(,-,);
ST(*n-);
for(;q>;q--)
{
scanf("%d%d",&s,&t);
int croot=LCA(s,t);
if (croot==) printf("Not connected\n");
else printf("%d\n",dir[s]+dir[t]-*dir[croot]);
}
}
return ;
}

hdu 2874 Connections between cities(st&rmq LCA)的更多相关文章

  1. HDU 2874 Connections between cities(LCA)

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

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

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

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

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

  4. HDU 2874 Connections between cities(LCA+并查集)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=2874 [题目大意] 有n个村庄,m条路,不存在环,有q个询问,问两个村庄是否可达, 如果可达则输出 ...

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

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

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

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

  7. HDU 2874 Connections between cities(LCA Tarjan)

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

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

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

  9. HDU——2874 Connections between cities

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

随机推荐

  1. Easyui 官网网址

    http://www.jeasyui.com/download/list.php 下载版本1.5.2的easyui中文API,可在CSDN网站http://download.csdn.net/down ...

  2. luogu4473 BZOJ2143 2011[国家集训队]飞飞侠

    题目戳这里 有问题可以在博客@ 应该还会有人来看吧,嘻嘻 正题: 题目大意: 题目很清楚,就是一个点有一定的范围,会有一定的花费 求三个点中的任意两个点到另一个点的最小花费 (麻麻教育我千万读好题目( ...

  3. SPOJ Hacking(字典树 + 搜索)题解

    思路1:字典树存每个串,然后dfs遍历是否存在.这里有个技巧,如果每次都重新初始化字典树为-1,那么会超时,所以我先初始化为-1,然后设一个Case,每个test时Case都++,那么只要开一个数组判 ...

  4. HDU 6148 Valley Numer (数位DP)题解

    思路: 只要把status那里写清楚就没什么难度T^T,当然还要考虑前导零! 代码: #include<cstdio> #include<cstring> #include&l ...

  5. sql server文件另存为的时候,选择文件编码和换行

    文件编码 使用code page来标记的,没有找到utf8 without bom 换行

  6. Gym - 100342J Triatrip (bitset求三元环个数)

    https://vjudge.net/problem/Gym-100342J 题意:给出一个邻接矩阵有向图,求图中的三元环的个数. 思路: 利用bitset暴力求解,记得最后需要/3. #includ ...

  7. 用EL時(el-api.jar,el-ri.jar ),要設isELIgnored="false"

    用EL時(el-api.jar,el-ri.jar ),要設isELIgnored="false" 否则jstl标签不显示. 加上 <%@page isELIgnored="false ...

  8. 20161208xlVBA工作表数据导入Access

    Sub InsertToDataBase() Dim DataPath As String Dim SQL As String Const DataName As String = "yun ...

  9. docker添加国内仓库安装iredmail

    centos 7: 1.yum install docker or yum update docker sudo tee /etc/docker/daemon.json <<-'EOF'{ ...

  10. Jersey 2.x 前言和约定的文本格式

    这是Jersey 2.x 的用户指南.我们极力将它能与我们新增的功能保持一致.当阅读本指南,作为补充,也请移步至Jersey API documentation查看 Jersey 的特性和 API. ...