HDU2874-LCA-离线targan
2017-01-15 15:09
141 查看
Connections between cities
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10158 Accepted Submission(s): 2428
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 o
4000
f 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条边(可能是森林)q次询问,查询点a到点b的最短距离,不能到达输出Not connected
题目思路:LCA模板题,用个dis数组记录点到根节点的距离,所以每次询问是的距离就是 dis[u]+dis[v] - 2*dis[getfather(v)]
AC代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 1e4+50;
const int maxm = 1e4+50;
const int maxq = 1e6+50;
class Targan{
public :
int n,m,q;
int e_hed[maxn],q_hed[maxn],top_e,top_q,vis[maxn],dis[maxn],pre[maxn],ans[maxq];
struct e_st{
int v,w,nex;
}e_Edge[maxm*2];
struct q_st{
int v,id,nex;
}q_Edge[maxq*2];
int getfather(int x){
if(x!=pre[x])return pre[x] = getfather(pre[x]);
return pre[x];
}
void e_add(int u,int v,int w){
e_Edge[top_e].v = v,e_Edge[top_e].w = w,e_Edge[top_e].nex = e_hed[u],e_hed[u]=top_e++;
}
void q_add(int u,int v,int i){
q_Edge[top_q].v = v,q_Edge[top_q].id = i,q_Edge[top_q].nex = q_hed[u],q_hed[u]=top_q++;
}
void init(){
top_e = top_q = 0;
memset(e_hed,-1,sizeof(e_hed));
memset(q_hed,-1,sizeof(q_hed));
memset(ans,-1,sizeof(ans));
memset(vis,0,sizeof(vis));
}
void in(){
while(~scanf("%d%d%d",&n,&m,&q)){
init();
for(int i =0;i<m;i++){
int u,v,w;scanf("%d%d%d",&u,&v,&w);
e_add(u,v,w);
e_add(v,u,w);
}
for(int i=0;i<q;i++){
int u,v;scanf("%d%d",&u,&v);
q_add(u,v,i);
q_add(v,u,i);
}
sove();
out();
}
}
void out(){
for(int i=0;i<q;i++){
if(ans[i]==-1)printf("Not connected\n");
else printf("%d\n",ans[i]);
}
}
void targan(int s,int deep,int root){
pre[s] = s;
dis[s] = deep;
vis[s] = root;
for(int i=e_hed[s];~i;i=e_Edge[i].nex){
int v = e_Edge[i].v;
if(vis[v]==0){
targan(v,deep+e_Edge[i].w,root);
pre[v] = s;
}
}
for(int i = q_hed[s];~i;i=q_Edge[i].nex){
int v = q_Edge[i].v;
if(vis[v]!=root)continue;
ans[q_Edge[i].id] = dis[s]+dis[v] - 2*dis[getfather(v)];
}
}
void sove(){
for(int i=1;i<=n;i++){
if(vis[i]==0){
targan(i,0,i);
}
}
}
}tg;
int main()
{
tg.in();
return 0;
}
相关文章推荐
- hdu2874(lca / tarjan离线 + RMQ在线)
- hdu2874 Connections between cities (LCA离线)
- HDU 2586 How far away ? LCA离线tarjan思想
- HDU 2874 Connections between cities (离线LCA)
- poj 1470 Closest Common Ancestors (离线LCA Tarjan)
- lca 在线,离线 poj 1330
- hdu 2586 How far away ?(在线LCA+离线Tarjan)
- poj 1330 lca-targan离线算法
- LCA 最近公共祖先——Tarjan(离线)算法的基本思路及其算法实现
- 09-09 HDU_Steps5.3 树状数组,LCA HDU1166 HDU2492 HDU3584 HDU2586 HDU2874 HDU3486 HDU2688
- 【HDU2586】How far away ?【离线】【TarjanLCA】
- POJ:1330-Nearest Common Ancestors(LCA在线、离线、优化算法)
- HDU4547 CD操作(最近公共祖先lca,离线Tarjan,有坑点)
- [HDOJ2874]Connections between cities(LCA, 离线tarjan)
- LCA 最近公共祖先 tarjan离线 总结 结合3个例题
- HDU 3078 - Network (LCA)【tarjan离线/DFS倍增】
- poj 1986 Distance Queries(LCA:倍增/离线)
- POJ 1470 Closest Common Ancestors (离线LCA,4级)
- 3626: [LNOI2014]LCA (树链剖分+离线处理)
- HDU2586How far away ?(LCA-在线ST+ tarjan离线)