How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 19879 Accepted Submission(s): 7786
Problem Description
There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it hard to answer. But luckily int this
village the answer is always unique, since the roads are built in the way that there is a unique simple path("simple" means you can't visit a place twice) between every two houses. Yout task is to answer all these curious people.
Input
First line is a single integer T(T<=10), indicating the number of test cases.
For each test case,in the first line there are two numbers n(2<=n<=40000) and m (1<=m<=200),the number of houses and the number of queries. The following n-1 lines each consisting three numbers i,j,k, separated bu a single space, meaning that there is a road connecting house i and house j,with length k(0<k<=40000).The houses are labeled from 1 to n.
Next m lines each has distinct integers i and j, you areato answer the distance between house i and house j.
For each test case,in the first line there are two numbers n(2<=n<=40000) and m (1<=m<=200),the number of houses and the number of queries. The following n-1 lines each consisting three numbers i,j,k, separated bu a single space, meaning that there is a road connecting house i and house j,with length k(0<k<=40000).The houses are labeled from 1 to n.
Next m lines each has distinct integers i and j, you areato answer the distance between house i and house j.
Output
For each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case.
Sample Input
2 3 2 1 2 10 3 1 15 1 2 2 3 2 2 1 2 100 1 2 2 1
Sample Output
10 25 100 100思路:LCA模板题代码:#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #include <stack> #include <map> using namespace std; const int inf=0x3f3f3f3f; const int M=100010; const int N=100010; int head[N],h[N],dis[N],f[N],a[N]; bool in[N],vis[N]; int cnt,num,tt; string s1,s2; int n,m; map<string,int>mp; struct node { int u,v,w,next; }e[M<<1],q[M<<1]; void addedge(int u,int v,int w) { e[num].v=v; e[num].w=w; e[num].next=head[u]; head[u]=num++; } void addque(int u,int v,int w) { q[tt].v=v; q[tt].w=w; q[tt].next=h[u]; h[u]=tt++; q[tt].v=u; q[tt].w=w; q[tt].next=h[v]; h[v]=tt++; } void init() { memset(head,-1,sizeof(head)); memset(h,-1,sizeof(h)); memset(in,false,sizeof(in)); memset(vis,false,sizeof(vis)); memset(f,-1,sizeof(f)); memset(dis,0,sizeof(dis)); cnt=num=tt=0; } int findx(int x) { return f[x]==-1?x:f[x]=findx(f[x]); } void unio(int x,int y) { int xx=findx(x); int yy=findx(y); if(yy!=xx)f[yy]=xx; } void tarjan(int u) { int i,v; for(i=head[u];i!=-1;i=e[i].next) { v=e[i].v; if(vis[v])continue; dis[v]=dis[u]+e[i].w; tarjan(v); unio(u,v); } vis[u]=true; for(i=h[u];i!=-1;i=q[i].next) { v=q[i].v; if(vis[v]) { a[q[i].w]=dis[u]-dis[findx(u)]+dis[v]-dis[findx(v)]; } } } int main() { int T; cin>>T; while(T--) { init(); scanf("%d%d",&n,&m); int i; int u,v,w; for(i=0;i<n-1;i++) { scanf("%d%d%d",&u,&v,&w); addedge(u,v,w); in[v]=true; } for(i=0;i<m;i++) { scanf("%d%d",&u,&v); addque(u,v,i); } for(i=1;i<=n;i++) { if(!in[i]) { tarjan(i); break; } } for(i=0;i<m;i++) { printf("%d\n",a[i]); } } return 0; }