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.

#include <iostream>
#include <vector>
#include <stack>
#include <cstring>
#include <cstdio>
#include <memory.h>
#include<vector>
using namespace std;
int Laxt[],Next[],To[],Len[];
int Laxt2[],Next2[],To2[],ans[];
bool vis[];
int cnt,cnt2;
int dis[],fa[];
void _update()
{
memset(Laxt,-,sizeof(Laxt));
memset(Laxt2,-,sizeof(Laxt2));
memset(vis,false,sizeof(vis));
cnt=cnt2=;
}
void _add(int u,int v,int d){
Next[cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
Len[cnt++]=d;
}
void _add2(int u,int v){
Next2[cnt2]=Laxt2[u];
Laxt2[u]=cnt2;
To2[cnt2++]=v;
Next2[cnt2]=Laxt2[v];
Laxt2[v]=cnt2;
To2[cnt2++]=u;
}
int _findfa(int v){
if(v==fa[v]) return fa[v];
return fa[v]=_findfa(fa[v]);
}
void _tarjan(int v)
{
vis[v]=true;fa[v]=v;
for(int i=Laxt[v];i!=-;i=Next[i]){
if(!vis[To[i]]){
dis[To[i]]=dis[v]+Len[i];
_tarjan(To[i]);
fa[To[i]]=v;
}
}
for(int i=Laxt2[v];i!=-;i=Next2[i]){
if(vis[To2[i]]){
int tmp=_findfa(To2[i]);
if(dis[To2[i]]!=-)
ans[i/]=dis[v]+dis[To2[i]]-*dis[tmp];
else ans[i/]=-;
}
}
}
int main()
{
int n,m,c,i,x,y,z;
while(~scanf("%d %d %d",&n,&m,&c)){
_update();
for(i=;i<m;i++){
scanf("%d%d%d",&x,&y,&z);
_add(x,y,z);
_add(y,x,z);
}
for(i=;i<c;i++){
scanf("%d%d",&x,&y);
_add2(x,y);
}
for(i=;i<=n;i++){
if(!vis[i]){
memset(dis,-,sizeof(dis));
dis[i]=;
_tarjan(i);
}
}
for(i=;i<c;i++)
if(ans[i]==-) printf("Not connected\n");
else printf("%d\n",ans[i]);
}
return ;
}

HDU2874Connections between cities( LCA )Tarjan的更多相关文章

  1. 最近公共祖先(LCA)---tarjan算法

    LCA(最近公共祖先).....可惜我只会用tarjan去做 真心感觉tarjan算法要比倍增算法要好理解的多,可能是我脑子笨吧略略略 最近公共祖先概念:在一棵无环的树上寻找两个点在这棵树上深度最大的 ...

  2. luogu3379 【模板】最近公共祖先(LCA) Tarjan

    LCA的Tarjan算法是一个离线算法,复杂度$O(n+q)$. 我们知道Dfs搜索树时会形成一个搜索栈.搜索栈顶节点cur时,对于另外一个节点v,它们的LCA便是v到根节点的路径与搜索栈开始分叉的那 ...

  3. [HDOJ2874]Connections between cities(LCA, 离线tarjan)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 这题有不连通的情况,特别注意. 觉得是存query的姿势不对,用前向星存了一遍,还是T…… /* ...

  4. 洛谷 P3379 【模板】最近公共祖先(LCA)Tarjan离线

    题目链接:LCA tarjan离线 这道题目WA无数发,最后还是参考了大神的blog 谁会想到因为一个输入外挂WA呢 大概是我的挂是假挂吧...orz(其实加上外挂,速度提升很多) 用链式前向星保存边 ...

  5. Connections between cities(LCA)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 题目: Problem Description After World War X, a lot ...

  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)

    dfs找出所有节点所在树及到树根的距离及深度及父亲. i和j在一棵树上,则最短路为dis[i]+dis[j]-dis[LCA(i,j)]*2. #include <cstring> #in ...

  8. 洛谷P3379 【模板】最近公共祖先(LCA)

    P3379 [模板]最近公共祖先(LCA) 152通过 532提交 题目提供者HansBug 标签 难度普及+/提高 提交  讨论  题解 最新讨论 为什么还是超时.... 倍增怎么70!!题解好像有 ...

  9. 图论--最近公共祖先问题(LCA)模板

    最近公共祖先问题(LCA)是求一颗树上的某两点距离他们最近的公共祖先节点,由于树的特性,树上两点之间路径是唯一的,所以对于很多处理关于树的路径问题的时候为了得知树两点的间的路径,LCA是几乎最有效的解 ...

随机推荐

  1. 破解NET的四大神器(转)

    原文地址 原本这篇文章可以更早一星期写出来与大家分享,由于某方面的原因耽搁到现在,心里竟有那么一点好像对不住大家的感觉.这当然与神器有关,因为我发现利用这四大神器我似乎觉得几乎所有的NET程序破解都不 ...

  2. Django小项目web聊天

    WEBQQ的实现的几种方式 1.HTTP协议特点 首先这里要知道HTTP协议的特点:短链接.无状态! 在不考虑本地缓存的情况举例来说:咱们在连接博客园的时候,当tcp连接后,我会把我自己的http头发 ...

  3. CCPC 2017-2018, Finals Solution

    A - Dogs and Cages 水. #include <bits/stdc++.h> using namespace std; int t; double n; int main( ...

  4. uwsgi手动安装时报错ValueError: invalid literal for int() with base 10: '32_1'

    安装uwsgi,安装步骤如下 wget https://projects.unbit.it/downloads/uwsgi-latest.tar.gz tar zxvf uwsgi-latest.ta ...

  5. 对Spring Bean了解一二

    这之前从未听说过Spring Bean,今天因为学习的<Java核心36讲>其中一篇涉及到了这个内容,因自己基础薄弱,杨晓峰老师讲的内容需要一定的基础才能看懂,故在网上搜罗一些我能理解的关 ...

  6. Refactoring #002 Inline Method

    Example private ServerSocket createServerSocket(final int port) throws IOException { ServerSocket re ...

  7. P1757 通天之分组背包 / hdu1712 ACboy needs your help (分组背包入门)

    P1757 通天之分组背包 hdu1712 ACboy needs your help hdu1712题意:A[i][j]表示用j天学习第i个课程能够得到A[i][j]的收益,求m天内获得的收益最大值 ...

  8. Hadoop运维手记

    1.处理hadoop的namenode宕机 处理措施:进入hadoop的bin目录,重启namenode服务 操作命令:cd path/to/hadoop/bin ./hadoop-daemon.sh ...

  9. postman中如何使用OAuth

    https://learning.getpostman.com/docs/postman/sending_api_requests/authorization/ Authorization The a ...

  10. Java Spring-AspectJ

    2017-11-10 21:25:02 Spring的AspectJ的AOPAspectJ 是一个面向切面的框架,它扩展了 Java 语言. AspectJ 定义了 AOP 语法所以它有一个专门的编译 ...