HDU 2586 How far away ?

版权声明:原创文章,转载请注明出处。本博新地址www.iaccepted.net https://blog.csdn.net/IAccepted/article/details/43072689

How far away ?

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


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
 


题目的意思就是给定一些houses的编号,及这些编号房子之间的距离,然后有m次询问,每次询问给出两个房子的编号,要求给出这两个房子的最小距离。这张图是树图,也就是总共N个点N-1条边,然后能把所有点全部连通。本身分析题目很容易知道本题就是求最小公共祖先的问题,首先确定一个根节点,然后DFS遍历一遍计算此根节点到所有节点的距离,然后可以用离线的tarjan算法,来找到两个询问节点a和b的最近公共祖先c,然后要求的结果就是dist[a] - dist[c] + dist[b] - dist[c],意思很好理解,画棵树看看就可以了,而且这种思路也是很容易想到的。这里用了一下非递归的DFS直接来求,每次询问直接使用DFS求亮点之间的距离,也可以很轻松的AC。

#include <cstdio>
#include <vector>
#include <stack>
#include <string>

using namespace std;

const int N = 40005;

int n, m, t;
vector<int> adj[N];
vector<int> wei[N];
int dist[N];
bool visit[N];

void init(int n)
{
	for (int i = 0; i <= n; ++i)
	{
		adj[i].clear();
		wei[i].clear();
	}
}

int dfs(int x, int y)
{
	int res = 0;

	memset(visit, 0, sizeof(visit));
	memset(dist, 0, sizeof(dist));

	stack<int> st;
	st.push(x);
	visit[x] = true;

	while (!st.empty())
	{
		int tx = st.top();
		st.pop();

		if (tx == y)break;

		for (int i = 0; i < adj[tx].size(); ++i)
		{
			int ty = adj[tx][i];
			if (visit[ty])continue;
			st.push(ty);
			visit[ty] = true;

			dist[ty] = dist[tx] + wei[tx][i];
		}
	}
	return dist[y];
}

int main()
{
	int a, b, w;

	scanf("%d", &t);

	while (t--)
	{
		scanf("%d %d", &n, &m);

		init(n);

		for (int i = 0; i < n - 1; ++i)
		{
			scanf("%d %d %d", &a, &b, &w);
			adj[a].push_back(b);
			adj[b].push_back(a);
			wei[a].push_back(w);
			wei[b].push_back(w);
		}

		for (int i = 0; i < m; ++i)
		{
			scanf("%d %d", &a, &b);
			printf("%d\n", dfs(a, b));
		}
		
		if (t != 0)printf("\n");
	}
	return 0;
}


欢迎访问本人另一个博客 www.iaccepted.net

猜你喜欢

转载自blog.csdn.net/IAccepted/article/details/43072689