Connections between cities

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

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
 
 
题意:
n个点m条边,不存在环,也就是说要么是树要么就是多棵树,c次查询,问x到y的距离。
思路:
由于可能查询的2个点不相连,可以给同一棵树中的点一个标记,如果查询的时候2个点不属于同一棵树,那肯定就不相连。
 
/*
* Author: sweat123
* Created Time: 2016/7/13 10:56:50
* File Name: main.cpp
*/
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define key_value ch[ch[root][1]][0]
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
struct node{
int to;
int val;
int next;
}edge[MAXN*];
int dp[MAXN*][],ver[MAXN*],vis[MAXN],dfn[MAXN*],first[MAXN],pre[MAXN];
ll dis[MAXN];
int n,m,q,tot,ind,mark[MAXN];
void add(int x,int y,int z){
edge[ind].to = y;
edge[ind].val = z;
edge[ind].next = pre[x];
pre[x] = ind ++;
}
void dfs(int rt,int dep,int flag){
vis[rt] = ;
ver[++tot] = rt;
dfn[tot] = dep;
first[rt] = tot;
mark[rt] = flag;
for(int i = pre[rt]; i != -; i = edge[i].next){
int t = edge[i].to;
if(!vis[t]){
dis[t] = dis[rt] + edge[i].val;
dfs(t,dep+,flag);
ver[++tot] = rt;
dfn[tot] = dep;
}
}
}
void rmq(){
for(int i = ; i <= tot; i++){
dp[i][] = i;
}
for(int i = ; i < ; i++){
for(int j = ; j + ( << i) - <= tot; j++){
int x = dp[j][i-];
int y = dp[j+(<<(i-))][i-];
if(dfn[x] > dfn[y]){
dp[j][i] = y;
} else{
dp[j][i] = x;
}
}
}
}
int askrmq(int x,int y){
x = first[x];
y = first[y];
if(x > y)swap(x,y);
int k = (int)(log(y - x + ) * 1.0 / log(2.0));
int l = dp[x][k];
int r = dp[y - (<<k) + ][k];
if(dfn[l] > dfn[r])return r;
else return l;
}
int main(){
while(~scanf("%d%d%d",&n,&m,&q)){
ind = tot = ;
memset(vis,,sizeof(vis));
memset(pre,-,sizeof(pre));
for(int i = ; i <= m; i++){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
int cnt = ;
memset(mark,,sizeof(mark));
for(int i = ; i <= n; i++){
if(!vis[i])dfs(i,,++cnt);
}
rmq();
while(q--){
int x,y;
scanf("%d%d",&x,&y);
if(mark[x] != mark[y]){
printf("Not connected\n");
} else{
int tp = ver[askrmq(x,y)];
ll ans = dis[x] - dis[tp] + dis[y] - dis[tp];
printf("%lld\n",ans);
}
}
}
return ;
}

hdu2874 LCA在线算法的更多相关文章

  1. LCA在线算法ST算法

    求LCA(近期公共祖先)的算法有好多,按在线和离线分为在线算法和离线算法. 离线算法有基于搜索的Tarjan算法较优,而在线算法则是基于dp的ST算法较优. 首先说一下ST算法. 这个算法是基于RMQ ...

  2. LCA在线算法详解

    LCA(最近公共祖先)的求法有多种,这里先介绍第一种:在线算法. 声明一下:下面的内容参考了http://www.cnblogs.com/scau20110726/archive/2013/05/26 ...

  3. LCA在线算法(hdu2586)

    hdu2586 How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  4. HDU 2586 How far away ?(LCA在线算法实现)

    http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵树,求出树上任意两点之间的距离. 思路: 这道题可以利用LCA来做,记录好每个点距离根结点的 ...

  5. hdu 2586 lca在线算法(朴素算法)

    #include<stdio.h> #include<string.h>//用c/c++会爆栈,用g++ac #define inf 0x3fffffff #define N ...

  6. POJ 1330 Nearest Common Ancestors (LCA,倍增算法,在线算法)

    /* *********************************************** Author :kuangbin Created Time :2013-9-5 9:45:17 F ...

  7. POJ - 1330 Nearest Common Ancestors(dfs+ST在线算法|LCA倍增法)

    1.输入树中的节点数N,输入树中的N-1条边.最后输入2个点,输出它们的最近公共祖先. 2.裸的最近公共祖先. 3. dfs+ST在线算法: /* LCA(POJ 1330) 在线算法 DFS+ST ...

  8. HDU2874(LCA应用:求两点之间距离,图不连通)

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

  9. LCA最近公共祖先 ST+RMQ在线算法

    对于一类题目,是一棵树或者森林,有多次查询,求2点间的距离,可以用LCA来解决.     这一类的问题有2中解决方法.第一种就是tarjan的离线算法,还有一中是基于ST算法的在线算法.复杂度都是O( ...

随机推荐

  1. iOS之微信支付

    前言:下面介绍微信支付的开发流程的细节,图文并茂,你可以按照我的随笔流程过一遍代码.包你也学会了微信支付.而且支付也是面试常问的内容. 正文: 1.首先在开始使用微信支付之前,有一些东西是开发者必须要 ...

  2. IOS开发札记(2015-08-20)

    View显示数据借助Model的一个比较好的理由也是因为:有时候从服务器获取的数据相同的value可能对应不同的key(服务端多人协作开发时经常会出现这种情况) 这里盗图描述一下使用Model的好处 ...

  3. Android http Request / Response ContentType

    客户端在进行http请求服务器的时候,需要告诉服务器请求的类型,服务器在返回给客户端的数据的时候,也需要告诉客户端返回数据的类型. 这个类型就是  ContentType  ,不同的ContentTy ...

  4. Http相关知识

    Http的无状态性 无状态是指,当浏览器发送请求给服务器的时候,服务器响应客户端的请求.但是当同一个浏览器再次发送请求给服务器的时候,服务器并不知道它就是刚才的浏览器.简单的说就是,服务器不会去记得你 ...

  5. java中文件的I/O操作

    java中文件的读写操作 (一) (1)java中文件的字节转成字符读操作 FileInputStream fStream = new FileInputStream("test.txt&q ...

  6. 利用split

    java.lang.string.splitsplit 方法将一个字符串分割为子字符串,然后将结果作为字符串数组返回.stringObj.split([separator,[limit]])strin ...

  7. ORA-04063: view "SYS.DBA_REGISTRY" has errors

    测试环境做了RMAN还原(从10.2.0.4.0 32bit 还原到 10.2.0.4.0 64bit)后,查询dba_registry系统视图时报如下错误 SQL> select  comp_ ...

  8. 笔记整理之 Bulk Insert

    之前2篇日志整理了BCP大致的用法,这次整理一下它的兄弟 Bulk Insert 的写法以及和bcp那边的结合的用法. 首先,Bulk Insert 语句要在连接了Sql Server 服务器之后才执 ...

  9. 【转】Java并发编程:深入剖析ThreadLocal

    来自: http://www.importnew.com/17849.html 想必很多朋友对ThreadLocal并不陌生,今天我们就来一起探讨下ThreadLocal的使用方法和实现原理.首先,本 ...

  10. 【转载】Markdown使用笔记

    献给写作者的 Markdown 新手指南 http://www.jianshu.com/p/q81RER 「简书」作为一款「写作软件」在诞生之初就支持了 Markdown,Markdown 是一种「电 ...