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字体

  2. Google C++单元测试框架GoogleTest---TestFixture使用

    一.测试夹具(Test Fixtures):对多个测试使用相同的数据配置 如果你发现自己写了两个或更多的测试来操作类似的数据,你可以使用测试夹具.它允许您为几个不同的测试重复使用相同的对象配置. 要创 ...

  3. JavaWeb结合七牛云存储搭建个人相册服务

    JavaWeb结合七牛云存储搭建个人相册服务 一.引言1. 课程概述 相信很多人都知道网站一般会有很多图片,对于小型网站来说,图片放在网站服务器上不算什么,但当图片数量很大时,会造成服务器很臃肿,相应 ...

  4. C#.NET万能数据库访问封装类(ACCESS、SQLServer、Oracle)

    using System; using System.Collections; using System.Collections.Specialized; using System.Data; usi ...

  5. ORACLE 9i 数据库体系结构图

    ORACLE 9i 的数据库体系结构图,非常的全面.系统.高屋建瓴的整体介绍了ORACLE 9i 的数据库体系结构.如果能全面了解.清晰梳理.深入掌握这些知识点,相信对你了解学习.深入研究ORACLE ...

  6. ORA-01157 & ORA-01110

    测试服务器做了RMAN还原后,发现告警日志文件有如下错误信息ORA-01110: data file 206: '/u04/epps/oradata/temp02.dbf'   Errors in f ...

  7. ORA-27125: unable to create shared memory segment

    平台环境   :  Oracle Linux Server release 5.7 x86_64 数据库版本 :  Oracle Database 10g Enterprise Edition Rel ...

  8. GL.IssuePluginEvent 发布插件事件

    Description 描述 Send a user-defined event to a native code plugin. 发送一个用户定义的事件到一个本地代码插件. Rendering in ...

  9. openstack api快速入门

    原文:http://my.oschina.net/guol/blog/105430 openstack官方有提供api供开发者使用,可以使用api做一些外围的小工具,用来简化对openstack的管理 ...

  10. 在 KVM 上安装 Win7 虚拟机

    之前都是在用Linux 虚机,现在有需要用到Win7 虚机,才发现在 KVM 上安装 Win7 的过程远比想象中的复杂.本文就把其过程做个简单总结. 1. 在 Virtual Machine Mana ...