Distance Queries
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 10604 | Accepted: 3746 | |
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.
* 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.
题目大意:
给定树结点之间的距离和对应关系,然后给出N个询问,给出两个结点求他们的最近距离。
解题思路:
本题可以先求出两个点的最近公共祖先,然后用一个dist数组更新每个点距离根结点的距离,用dfs就可以实现,写在了LCA里,然后求的结果的表达式就是dist[一个结点]+dist[另一个结点] - 2 * dist[最近公共祖先].
AC代码:
#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 100005;
int p[maxn];
int head[maxn];
int qhead[maxn];
struct Node
{
int to;
int next;
int w;
int lca;
};
int m,n;
bool vis[maxn];
Node edge[maxn];
Node qedge[maxn];
int cnt;
int qcnt;
int dist[maxn];
void init()
{
int i;
memset(p,0,sizeof(p));
memset(vis,false,sizeof(vis));
memset(head,-1,sizeof(head));
memset(qhead,-1,sizeof(qhead));
memset(edge,0,sizeof(edge));
memset(dist,0,sizeof(dist));
cnt = 0;
qcnt = 0;
}
void addEdge(int u,int v,int w)
{
edge[cnt].w = w;
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt++;
}
void addQEdge(int u,int v)
{
qedge[qcnt].to = v;
qedge[qcnt].next = qhead[u];
qhead[u] = qcnt++;
}
int find(int x)
{
if(p[x] != x)
p[x] = find(p[x]);
return p[x];
}
void lca(int u)
{
p[u] = u;
int k;
vis[u] = true;
for(k = head[u];k!=-1;k=edge[k].next)
{
if(!vis[edge[k].to])
{
dist[edge[k].to] = dist[u] + edge[k].w; //更新和根结点的距离
lca(edge[k].to);
p[edge[k].to] = u;
}
}
for(k=qhead[u];k!=-1;k=qedge[k].next)
{
if(vis[qedge[k].to])
{
qedge[k].w = dist[u] + dist[qedge[k].to] - 2 * dist[find(qedge[k].to)]; //u和根结点的距离 + qedge[k].to与根结点的距离 - 2 * 公共祖先和根结点的距离
//cout<<u<<" "<<qedge[k].to<<" "<<qedge[k].w<<endl;
qedge[k^1].w = qedge[k].w;
qedge[k].lca = find(qedge[k].to);
qedge[k^1].lca = qedge[k].lca;
//cout<<qedge[k].lca<<endl;
}
}
}
int main()
{
int i;
char c[2];
//freopen("1.txt","r",stdin);
while(scanf("%d%d",&n,&m) != EOF)
{
int num1,num2,length;
int que;
init();
for(i=0;i<m;i++)
{
scanf("%d%d%d%s",&num1,&num2,&length,c);
addEdge(num1,num2,length);
addEdge(num2,num1,length);
}
scanf("%d",&que);
for(i=0;i<que;i++)
{
scanf("%d%d",&num1,&num2);
addQEdge(num1,num2);
addQEdge(num2,num1);
}
for(i=1;i<=n;i++)
{
if(head[i] != -1)
{
//cout<<i<<endl;
lca(i);
break;
}
}
for(i=0;i<qcnt;i+=2)
{
printf("%d\n",qedge[i].w);
}
}
return 0;
}