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-水波效果

    一,效果图. 二,工程图. 三,代码. ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIVie ...

  2. RMAN备份失败: ORA-19502 & ORA-27072: File I/O error

    早上检查一ORACLE数据库的RMAN备份的邮件时,发现出现了ORA-27072: File I/O error等错误,具体信息如下所示: channel ORA_DISK_1: starting p ...

  3. 作业配置规范文档[MS SQL]

    作业配置规范文档(MS SQL) 文档类型 MS SQL数据库作业配置规范文档 创建日期 2015-07-30 版本变化 V3.0 修改记录 修改人 修改日期 版本 修改描述 潇湘隐者 2015-08 ...

  4. [AlwaysOn Availability Groups]CLUSTER.LOG(AG)

    CLUSTER.LOG(AG) 作为故障转移资源,在SQL Server和windows故障转移集群服务的资源DLL(hadrres.dll)之间有额外的内部交流,DLL无法被SQL Server监控 ...

  5. 把Tomcat注册为windows服务

    配置环境变量 JAVA_HOME=D:\java CLASSPATH=.;%JAVA_HOME%\lib; PATH=%JAVA_HOME%\bin; 提示:一般jre默认在jdk目录下%JAVA_H ...

  6. HTML5 data-* 自定义属性

    在jQuery的attr与prop提 到过在IE9之前版本中如果使用property不当会造成内存泄露问题,而且关于Attribute和Property的区别也让人十分头痛,在 HTML5中添加了da ...

  7. 网络抓包工具-Wireshark学习资料

    wireshark一个非常牛逼的网络抓包工具.转载一系列博文 一站式学习Wireshark(一):Wireshark基本用法 一站式学习Wireshark(二):应用Wireshark观察基本网络协议 ...

  8. 转《WF编程》笔记目录

    <WF编程>笔记目录 2008-03-18 09:33 by Windie Chai, 26803 阅读, 49 评论, 收藏, 编辑 WF笔记开始 <WF编程>系列之0 - ...

  9. 怎样用ZBrush对模型进行渲染

    关于如何使用ZBrush®3D图形绘制软件雕刻僵尸模型,Fisker老师用了6个章节共41课时,从人体躯干和骨骼雕刻,到衣服.鞋子制作,再到顶点着色,向大家一一展示了雕刻过程,其中分享了很多ZBrus ...

  10. 【2016-10-31】【坚持学习】【Day16】【MongoDB】【入门 -概念】

    MongoDB 概念解析 不管我们学习什么数据库都应该学习其中的基础概念,在mongodb中基本的概念是文档.集合.数据库,下面我们挨个介绍. 下表将帮助您更容易理解Mongo中的一些概念: SQL术 ...