[Blue Bridge Cup] Real Exam Training 2013 C++A Group Question 10 Minister’s Travel Expenses

Problem Description

Problem description A
long time ago, Kingdom T had an unprecedented prosperity. In order to better manage the country, the kingdom has built a large number of expressways to connect the capital and major cities in the kingdom.

In order to save money, the ministers of country T have formulated an excellent construction plan after thinking, so that any big city can be reached directly from the capital or indirectly through other big cities. At the same time, if you don't repeatedly pass through big cities, the plan to reach each big city from the capital is unique.

J is an important minister of country T. He patrols between major cities to appreciate the sentiments of the people. Therefore, non-stop from one city to another has become the most common thing J does. He has a purse to store travel expenses between cities.

The clever J found that if he does not stop to repair in a certain city, the travel expenses he spends during the continuous journey are related to the distance he has traveled, and he is walking the kilometer from the xth kilometer to the x+1th kilometer. Medium (x is an integer), his travel expenses are x+10 so much. In other words, it costs 11 to walk 1 kilometer, and 23 to walk 2 kilometers.

Minister J wants to know: What is the maximum amount of travel expenses that he might spend when starting from a certain city without taking a break and arriving in another city?

Input format
The first line of input contains an integer n, which represents the number of cities in Kingdom T including the capital

Cities are numbered sequentially starting from 1, and city 1 is the capital.

Next line n-1, describe the highway in country T (the highway in country T must be n-1)

Each line has three integers Pi, Qi, Di, indicating that there is a highway between the city Pi and the city Qi, and the length is Di kilometers.

Output format
Output an integer, which indicates how much the minister J spends the most on the road.

Sample input 1
5
1 2 2
1 3 1
2 4 5
2 5 4
Sample output 1
135
output format
Minister J from city 4 to city 5 will cost 135 tolls.

Problem analysis

Violent recursion 25 points

#include <iostream>
#include <algorithm> 
using namespace std;
/*
样例输入1
5
1 2 2
1 3 1
2 4 5
2 5 4
样例输出1
135
*/

int n;
int res;

class Point{
public: 
	int num;//点的编号 
	int cost;//到该点距离 
};
   
void dfs(vector<Point> m[], int vis[], int i, int j, int dis){
	
	//是否有直接邻居 
	for(int k = 0 ; k < m[i].size(); k++){
		if(m[i][k].num == j){	//m[i]的直接邻居有j 
			res = max(res, dis+m[i][k].cost);
			return;
		}
	}	
	vis[i] = 0;
	//递归,不是直接邻居, 
	for(int k = 0; k < m[i].size(); k++){
		if(vis[m[i][k].num] == -1){
			dfs(m, vis, m[i][k].num, j, dis+m[i][k].cost);
		}
	} 
	vis[i] = -1;
}

int dis2money(int dis){
	return (11 + 10 + dis)*dis/2; 
} 

int main(int argc, char** argv) {
	cin >> n;
	vector<Point> m[n+1]; //m[i]表示i点的邻居集合 
	int vis[n+1] = {-1};
	
	for(int i = 1; i < n; i++){
		int a, b, c;
		cin >> a >> b >>c;
		Point p1 = {b, c};
		Point p2 = {a, c};
		m[a].push_back(p1);
		m[b].push_back(p2);
	}
	
	for(int i = 1; i <= n-1; i++){
		for(int j = i+1; j <= n; j++){
			dfs(m, vis, i, j, 0);
		}
	} 
//	cout << res << endl;
	cout << dis2money(res) << endl;
	return 0;
}

Method two twice dfs 

#include <iostream>
#include <algorithm> 
using namespace std;
/*
样例输入1
5
1 2 2
1 3 1
2 4 5
2 5 4
样例输出1
135
*/

int n;
int res;
int pnt = -1;

class Point{
public: 
	int num;//点的编号 
	int cost;//到该点距离 
};

void dfs(vector<Point> m[], int vis[], int start, int dis){
	vector<Point> nei_i = m[start];
	vis[start] = 0;
	bool isLeaf = true;
	
	for(int k = 0; k < nei_i.size(); k++){
		int num = nei_i[k].num;
		if(vis[num] == -1){
			isLeaf = false;
			dfs(m, vis, num, dis + nei_i[k].cost);
		}
	} 
	vis[start] = -1;
	if(isLeaf){
		if(dis > res){
			res = dis;
			pnt = start;
		}
	}
}
int dis2money(int dis){
	return (11 + 10 + dis)*dis/2; 
} 

int main(int argc, char** argv) {
	cin >> n;
	vector<Point> m[n+1]; //m[i]表示i点的邻居集合 
	int vis[n+1] = {-1};
	
	for(int i = 0; i < n-1; i++){
		int a, b, c;
		cin >> a >> b >>c;
		Point p1 = {b, c};
		Point p2 = {a, c};
		m[a].push_back(p1);
		m[b].push_back(p2);
	}
	
	dfs(m , vis, 1, 0);
	res = 0;
	dfs(m, vis, pnt, 0);
	cout << pnt;
//	cout << dis2money(res) << endl;
	return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_44566432/article/details/115180761