您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: