HDU_How far away ?

#include<iostream>  
#include<sstream>        
#include<string>        
#include<vector>        
#include<list>        
#include<set>        
#include<map>        
#include<stack>        
#include<queue>        
#include<algorithm>        
#include<cmath>        
#pragma warning(disable:4996)        
using namespace std;
class Edge
{
public:
	int next;
	int weight;
	Edge(int a = 0, int b = 0)
	{
		next = a, weight = b;
	}
};
class Graph
{
public:
	vector<vector<Edge>>adjList;
	Graph(const int &node_num)
	{
		adjList.resize(40010);
		for (int i = 0; i != node_num - 1; i++)
		{
			int a, b, weight; cin >> a >> b >> weight;
			adjList[a].push_back({ b,weight });
			adjList[b].push_back({ a,weight });
		}
	}
	int BFS(const int &start,const int &end)
	{
		queue<int>Q_path, Q_node;
		vector<bool>visited(40010);
		visited[start] = true;
		for (int i = 0; i != adjList[start].size(); i++)
		{
			Q_node.push(adjList[start][i].next);
			Q_path.push(adjList[start][i].weight);
		}
		while (true)
		{
			auto curnode = Q_node.front(); Q_node.pop();
			auto curpath = Q_path.front(); Q_path.pop();
			if (curnode == end)
			{
				return curpath;
			}
			for (int i = 0; i != adjList[curnode].size(); i++)
			{
				if (!visited[adjList[curnode][i].next])
				{
					Q_node.push(adjList[curnode][i].next);
					Q_path.push(adjList[curnode][i].weight + curpath);
					visited[adjList[curnode][i].next] = true;
				}
			}
		}
		return 0;
	}
};
int main()
{
	//freopen("input.txt", "r", stdin);  
	//freopen("output.txt", "w", stdout);  
	int T; cin >> T;
	while (T--)
	{
		int n, m; cin >> n >> m;
		Graph graph(n);
		while (m--)
		{
			int a, b; cin >> a >> b;
			cout << graph.BFS(a,b) << endl;
		}
	}
	return 0;
}
#include<iostream>  
#include<sstream>        
#include<string>        
#include<vector>        
#include<list>        
#include<set>        
#include<map>        
#include<stack>        
#include<queue>        
#include<algorithm>        
#include<cmath>        
#pragma warning(disable:4996)        
using namespace std;
int n, cnt, icnt;
int head[80000 + 100], l[80000 + 100], ll[80000 + 100], mark[80705], depth[80000 + 100], dist[80000 + 100];
int dp[80000 + 100][30];
class Node
{
public:
	int to, next, weight;
};
Node e[80000 + 100];
void addEdge(const int &a,const int &b,const int &w)
{
	e[cnt].to = b;
	e[cnt].next = head[a];
	e[cnt].weight = w;
	head[a] = cnt++;
}
void dfs(int u, int d)
{
	mark[u] = 1;
	l[++icnt] = u;
	ll[u] = icnt;
	depth[icnt] = d;
	for (int i = head[u]; i + 1; i = e[i].next)
	{
		int to = e[i].to;
		if (mark[to])
			continue;
		int w = e[i].weight;
		dist[to] = dist[u] + w;
		dfs(to, d + 1);
		l[++icnt] = u;
		depth[icnt] = d;
	}
}
void RMQ()
{
	int i, j;
	for (i = 1; i <= icnt; i++)
		dp[i][0] = i;
	for (i = 1; (1 << i) <= icnt; i++)
	{
		for (j = 1; j + (1 << i) <= icnt; j++)
		{
			int a = dp[j][i - 1], b = dp[j + (1 << i - 1)][i - 1];
			dp[j][i] = depth[a]<depth[b] ? a : b;
		}
	}
}
int St(int a, int b)
{
	int k = (int)((log((double)(b - a + 1))) / log(2.0));
	int x = dp[a][k], y = dp[b - (1 << k) + 1][k];
	int t = depth[x]<depth[y] ? x : y;
	return t;
}
int Lca(int a, int b)
{
	int x = ll[a], y = ll[b];
	if (x>y) swap(x, y);
	int st = St(x, y);
	return l[st];
}

int main()
{
	int T, m, a, b, c, i;
	scanf("%d", &T);
	while (T--)
	{
		memset(head, -1, sizeof(head));
		memset(mark, 0, sizeof(mark));
		cnt = 0;
		scanf("%d%d", &n, &m);
		for (i = 1; i <= n - 1; i++)
		{
			scanf("%d%d%d", &a, &b, &c);
			addEdge(a, b, c);
			addEdge(b, a, c);
		}
		icnt = 0;
		dfs(1, 1);
		RMQ();
		while (m--)
		{
			scanf("%d%d", &a, &b);
			int lca = Lca(a, b);
			printf("%d\n", dist[b] + dist[a] - 2 * dist[lca]);
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值