可惜没如果=_=
时光的河入海流

How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13641    Accepted Submission(s): 5117


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.
 

 

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
 

 

Source
 

 

Recommend
lcy   |   We have carefully selected several similar problems for you:  3486 2874 2888 3234 2818 
 
玛德这题为什么总是TLE……不是很懂>...<
 1 #include "bits/stdc++.h"
 2 #define mem(a,b) memset(a,b,sizeof(a))
 3 using namespace std;
 4 typedef long long LL;
 5 const int MAX1=40005;
 6 const int MAX2=405;
 7 int cas;
 8 int n,m;
 9 int tot,tq;
10 int head[MAX1],hq[MAX1];
11 struct Edge{ int v,w; int next;} edge[MAX2],eq[MAX2];
12 int ans[MAX2],dis[MAX1];
13 int fa[MAX1];
14 bool vis[MAX1];
15 inline void addedge(int u,int v,int w) {tot++; edge[tot].v=v;edge[tot].w=w; edge[tot].next=head[u]; head[u]=tot;}
16 inline void addq(int u,int v) {tq++; eq[tq].v=v; eq[tq].next=hq[u]; hq[u]=tq;}
17 int getfather(int x) {if (fa[x]==x) return x;return fa[x]=getfather(fa[x]);}
18 void dfs(int x,int ff,int w){
19     dis[x]=w;
20     for (int i=head[x];i;i=edge[i].next){
21         if (edge[i].v==ff) continue;
22         dfs(edge[i].v,x,w+edge[i].w);
23     }
24 }
25 void init(){
26     int i,j;
27     int u,v,w;
28     scanf("%d%d",&n,&m);
29     mem(ans,0),mem(vis,false),mem(head,0),mem(hq,0),mem(dis,0);
30     for (i=1;i<=n;i++)
31      fa[i]=i;
32     tot=tq=0;
33     for (i=1;i<n;i++){
34         scanf("%d%d%d",&u,&v,&w);
35         addedge(u,v,w);
36         addedge(v,u,w);
37     }
38     for (i=1;i<=m;i++){
39         scanf("%d%d",&u,&v);
40         addq(u,v);
41     }
42 }
43 void tarjanlca(int x){
44     fa[x]=x;vis[x]=true;
45     int i,j;
46     for (i=head[x];i;i=edge[i].next){
47         if (!vis[edge[i].v]){
48             tarjanlca(edge[i].v);
49             fa[edge[i].v]=x;
50         }
51     }
52     for (i=hq[x];i;i=eq[i].next){
53         if (vis[eq[i].v]){
54             j=getfather(eq[i].v);
55             ans[i]=dis[x]+dis[eq[i].v]-2*dis[j];
56         }
57     }
58 }
59 int main(){
60     freopen ("away.in","r",stdin);
61     freopen ("away.out","w",stdout);
62     int i,j;
63     scanf("%d",&cas);
64     while (cas--){
65         init();
66         dfs(1,0,0);
67         tarjanlca(1);
68         for (i=1;i<=m;i++)
69          printf("%d\n",ans[i]);
70     }
71     return 0;
72 }

 

posted on 2016-10-31 22:32  珍珠鸟  阅读(180)  评论(0编辑  收藏  举报