蓝桥杯2013年第四届C/C++ A组蓝桥杯省赛真题——大臣的旅费

本文介绍了一个关于树的直径问题的经典算法题目,通过两次广度优先搜索寻找树中最长路径,并给出了两种实现方式及其代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述
很久以前,T王国空前繁荣。为了更好地管理国家,王国修建了大量的快速路,用于连接首都和王国内的各大城市。
为节省经费,T国的大臣们经过思考,制定了一套优秀的修建方案,使得任何一个大城市都能从首都直接或者通过其他大城市间接到达。同时,如果不重复经过大城市,从首都到达每个大城市的方案都是唯一的。
J是T国重要大臣,他巡查于各大城市之间,体察民情。所以,从一个城市马不停蹄地到另一个城市成了J最常做的事情。他有一个钱袋,用于存放往来城市间的路费。
聪明的J发现,如果不在某个城市停下来修整,在连续行进过程中,他所花的路费与他已走过的距离有关,在走第x千米到第x+1千米这一千米中(x是整数),他花费的路费是x+10这么多。也就是说走1千米花费11,走2千米要花费23。
J大臣想知道:他从某一个城市出发,中间不休息,到达另一个城市,所有可能花费的路费中最多是多少呢?
输入格式:
输入的第一行包含一个整数n,表示包括首都在内的T王国的城市数
城市从1开始依次编号,1号城市为首都。
接下来n-1行,描述T国的高速路(T国的高速路一定是n-1条)
每行三个整数Pi, Qi, Di,表示城市Pi和城市Qi之间有一条高速路,长度为Di千米。
输出格式:
输出一个整数,表示大臣J最多花费的路费是多少。

样例输入:
5
1 2 2
1 3 1
2 4 5
2 5 4

样例输出:
135

样例说明:
大臣J从城市4到城市5要花费135的路费。

**

题解:

**
刚开始我是直接以每个城市为起点,都算一次最远距离(用bfs去遍历所有的点),果不其然,超时了,过了80%,代码如下:

#include<iostream>
#include<map>
#include<queue>
using namespace std;
int n;
map<pair<int,int>,int> ro_len;
queue<int> qu;
typedef struct mm * line;
struct mm{
	int city;
	line next;
};
void Insert(line graph[],int c1,int c2){
	line temp = new struct mm;
	temp->city = c2;
	temp->next = graph[c1];
	graph[c1] = temp;
}
int CountCost(int len){
	int cost = 0;
	int i;
	for(i=1; i<=len; i++){
		cost += i+10;
	}
	return cost;
}
int main(void){
	int i,j;
	cin >> n;
	line graph[n+1];
	for(i=0; i<=n; i++){
		graph[i] = NULL;
	}
	for(i=0; i<n-1; i++){
		int city1,city2;
		int len;
		cin >> city1 >> city2 >> len;
		Insert(graph,city1,city2);
		Insert(graph,city2,city1);
		pair<int,int> t1(city1,city2);
		pair<int,int> t2(city2,city1);
		ro_len[t1] = ro_len[t2] = len;
	}
	int max = 0;
	for(i=1; i<=n; i++){
		int vis[n+1];
		int maxm[n+1];
		for(j=0; j<=n; j++){
			vis[j] = maxm[j] = 0;
		}
		vis[i] = 1;
		qu.push(i);
		while(!qu.empty()){
			int cc = qu.front(); qu.pop();
			line temp = graph[cc];
			while(temp){
				if(!vis[temp->city]){
					pair<int,int> t(cc,temp->city);
					vis[temp->city] = 1;
					maxm[temp->city] = ro_len[t] + maxm[cc];
					qu.push(temp->city);
				}
				temp = temp->next;
			}
		}
		for(j=1; j<=n; j++){
			max = maxm[j]>max?maxm[j]:max;
		}
	}
	cout << CountCost(max) << endl;
	return 0;
}

后来看了别人的题解,发现这是一个树的直径的问题:
只需要BFS两遍:

BFS第一遍:从图中任意一点x开始,得到距离x最远的一点y;

BFS第二遍:从点y开始,得到距离点y最远的一点z;

第二遍得到的最远距离就是正确值。

然后我就稍微修改了下,第一遍都取得node = 1,从首都开始遍历,第一次找到y点,然后第二次BFS再找到距离y最远的z点,其中每次遍历我都使用maxm[i]这个数组取记录从起点到该城市i的距离,然后最后根据maxm[i]数组,求这次遍历从起点到哪个城市是最远的(用max和k记录)。

代码如下:

#include<iostream>
#include<map>
#include<queue>
using namespace std;
int n;
map<pair<int,int>,int> ro_len;
typedef struct mm * line;
struct mm{
	int city;
	line next;
};
void Insert(line graph[],int c1,int c2){
	line temp = new struct mm;
	temp->city = c2;
	temp->next = graph[c1];
	graph[c1] = temp;
}
int CountCost(int len){
	int cost = 0;
	int i;
	for(i=1; i<=len; i++){
		cost += i+10;
	}
	return cost;
}
int main(void){
	int i,j;
	cin >> n;
	line graph[n+1];
	for(i=0; i<=n; i++){
		graph[i] = NULL;
	}
	for(i=0; i<n-1; i++){
		int city1,city2;
		int len;
		cin >> city1 >> city2 >> len;
		Insert(graph,city1,city2);
		Insert(graph,city2,city1);
		pair<int,int> t1(city1,city2);
		pair<int,int> t2(city2,city1);
		ro_len[t1] = ro_len[t2] = len;
	}
	int node = 1;
	int max;
	for(i=0; i<2; i++){
		max = 0;
		int vis[n+1],maxm[n+1],k = 1;
		int cc;
		for(j=0; j<=n; j++){
			vis[j] = maxm[j] = 0;
		}
		queue<int> qu;
		vis[node] = 1;
		qu.push(node);
		while(!qu.empty()){
			cc = qu.front(); qu.pop();
			line temp = graph[cc];
			while(temp){
				if(!vis[temp->city]){
					vis[temp->city] = 1;
					pair<int,int> t(cc,temp->city);
					maxm[temp->city] = ro_len[t] + maxm[cc];
					qu.push(temp->city);
				}
				temp = temp->next;
			}
		}
		for(j=1; j<=n; j++){
			if(maxm[j] > max){
				k = j;
				max = maxm[j];
			}
		}
		node = k;
	}
	cout << CountCost(max) << endl;
	return 0;
}

**

其中有个特别要注意的问题(针对我自己):

**
对于树的直径问题:
如果这个树的边没有权值,树的直径当然是把树展开,树的高度就是树的直径;
如果这个树的边有权值,虽然方法还是之前提到的方法,但不要觉得就是树的高度,它可能是直接从这个点到一个相邻的点这一段就是树的直径。
不然想当然!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值