1. 程式人生 > >POJ-1986 Distance Queries

POJ-1986 Distance Queries

struct mar node pair height this -c ostream path

Distance Queries
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 13987 Accepted: 4924
Case Time Limit: 1000MS

Description

Farmer John‘s cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input as in "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries". Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two farms). Please answer FJ‘s distance queries as quickly as possible!

Input

* Lines 1..1+M: Same format as "Navigation Nightmare"

* Line 2+M: A single integer, K. 1 <= K <= 10,000

* Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms.

Output

* Lines 1..K: For each distance query, output on a single line an integer giving the appropriate distance.

Sample Input

7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
3
1 6
1 4
2 6

Sample Output

13
3
36

Hint

Farms 2 and 6 are 20+3+13=36 apart.
這題目別想多,跟方向沒關系,LCA。 關鍵在於怎麽將距離整出來,我們可以有這麽一個方程,
           ans[id]=pre[v].len+pre[u].len-2*pre[Find(v)].len;
用一個圖來表示就是這樣:

技術分享
pre[v].len就是1到3的距離,
pre[u].len就是1到4的距離。
pre[Find(v)].len就是1到2的距離。
這樣就很好解決了所有的問題了。

還有就是,重要的事情說三遍:並查集要壓縮路徑!並查集要壓縮路徑!並查集要壓縮路徑!
之前幾次忘了壓縮,直接TLE;一臉懵逼

附上AC代碼:
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<algorithm>
 5 #include<vector>
 6 using namespace std;
 7 struct Node{
 8     int f;
 9     int len;
10 }pre[50080];
11 vector<pair<int ,int > >query[50050];
12 vector<pair <int ,int > >vec[50100];
13 int ans[50050],vis[50100];
14 int Find(int x)
15 {
16     if(pre[x].f==x)
17     {
18         return x;
19     }
20     else
21     {
22         pre[x].f=Find(pre[x].f);
23         return pre[x].f;
24     }
25 }
26 int dfs(int u,int fa,int length)
27 {
28     pre[u].f=u;
29     pre[u].len=pre[fa].len+length;
30     vis[u]=1;
31     for(int i=0;i<vec[u].size();i++)
32     {
33         int v=vec[u][i].first;
34         int l=vec[u][i].second;
35         if(v==fa)   continue;
36         dfs(v,u,l);
37     }
38     for(int i=0;i<query[u].size();i++)
39     {
40         int v=query[u][i].first;
41         int id=query[u][i].second;
42         if(vis[v]==1)
43         {
44              ans[id]=pre[v].len+pre[u].len-2*pre[Find(v)].len;
45         }
46     }
47     pre[u].f=fa;
48 }
49 int main()
50 {
51     int n,m,x,y,l;
52     char c;
53     while(scanf("%d%d",&n,&m)!=EOF)
54     {
55         memset(vis,0,sizeof(vis));
56         for(int i=0;i<m;i++)
57         {
58             scanf("%d%d%d %c",&x,&y,&l,&c);
59             vec[x].push_back({y,l});
60             vec[y].push_back({x,l});
61             vis[y]=1;
62         }
63         int k;
64         cin>>k;
65         for(int i=0;i<k;i++)
66         {
67             scanf("%d%d",&x,&y);
68             query[x].push_back({y,i});
69             query[y].push_back({x,i});
70         }
71         for(int i=1;i<=n;i++)
72         {
73             if(vis[i]==0)
74             {
75                 memset(vis,0,sizeof(vis));
76                 dfs(i,-1,0);
77                 break;
78             }
79         }
80         for(int i=0;i<k;i++)
81             cout<<ans[i]<<endl;
82         memset(ans,0,sizeof(ans));
83         memset(pre,0,sizeof(pre));
84         for(int i=0;i<=n;i++)
85         {
86             vec[i].clear();
87             query[i].clear();
88         }
89     }
90     return 0;
91 }

 

POJ-1986 Distance Queries