How far away ?

How far away ?

http://acm.hdu.edu.cn/showproblem.php?pid=2586

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


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
 

 

 

在JV dalao的指导下,终于学会了tarjan。。。

推荐一下他写的关于tarjan的文章,很详细:http://www.cnblogs.com/JVxie/p/4854719.html

模板题

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <string>
 7 #include<vector>
 8 #include <queue>
 9 const int INF=0x3f3f3f3f;
10 #define maxn 40005
11 using namespace std;
12 
13 int n,m;
14 
15 vector<pair<int,int> >mp[maxn],book[maxn];
16 int fa[maxn];
17 int vis[maxn],dis[maxn];
18 int ans[maxn];
19 
20 int Find(int x){
21     int r=x,y;
22     while(x!=fa[x]){
23         x=fa[x];
24     }
25     while(r!=x){
26         y=fa[r];
27         fa[r]=x;
28         r=y;
29     }
30     return x;
31 }
32 
33 int join(int x,int y){
34     int xx=Find(x);
35     int yy=Find(y);
36     if(xx!=yy){
37         fa[yy]=xx;
38         return true;
39     }
40     return false;
41 }
42 
43 void tarjan(int x,int pre,int dist){
44     int to,len,tmp;
45     for(int i=0;i<mp[x].size();i++){
46         to=mp[x][i].first;
47         len=mp[x][i].second;
48         tmp=dist+len;
49         if(to!=pre&&!vis[to]){
50             dis[to]=tmp;
51             tarjan(to,x,tmp);
52             vis[to]=1;
53             join(x,to);
54         }  
55     }
56     int pos;
57     for(int i=0;i<book[x].size();i++){
58         to=book[x][i].first;
59         pos=book[x][i].second;
60         if(vis[to]&&ans[pos]==-1){
61             tmp=Find(to);
62             ans[pos]=dis[to]+dis[x]-2*dis[tmp];
63         }
64     }
65 }
66 
67 int main(){
68     std::ios::sync_with_stdio(false);
69     int T;
70     cin>>T;
71     while(T--){
72         memset(vis,0,sizeof(vis));
73         memset(ans,-1,sizeof(ans));
74         memset(dis,0,sizeof(dis));
75         for(int i=0;i<=n;i++){
76             mp[i].clear();
77             book[i].clear();
78         }
79         int x,y,z;
80         cin>>n>>m;
81         for(int i=0;i<=n;i++) fa[i]=i;
82         for(int i=1;i<n;i++){
83             cin>>x>>y>>z;
84             mp[x].push_back(make_pair(y,z));
85             mp[y].push_back(make_pair(x,z));
86         }
87         for(int i=1;i<=m;i++){
88             cin>>x>>y;
89             book[x].push_back(make_pair(y,i));
90             book[y].push_back(make_pair(x,i));
91         }
92         tarjan(1,-1,0);
93         for(int i=1;i<=m;i++){
94             cout<<ans[i]<<endl;
95         }
96     }
97     system("pause");
98 }
View Code

 

posted on 2018-11-22 22:28  Fighting_sh  阅读(240)  评论(0编辑  收藏  举报

导航