Timus 2005. Taxi for Programmers 题解

本文介绍了一个特定场景下的旅行商问题(TSP)实例,利用预设路径的方法,通过暴力搜索算法来寻找从训练室到家的最短路线。该问题限定在五个节点上,并排除了一种特定的路径顺序。

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

The clock shows 11:30 PM. The sports programmers of the institute of maths and computer science have just finished their training. The exhausted students gloomily leave their computers. But there’s something that cheers them up: Misha, the kind coach is ready to give all of them a lift home in his brand new car. Fortunately, there are no traffic jams on the road. Unfortunately, all students live in different districts. As Misha is a programmer, he highlighted and indexed five key points on the map of Yekaterinburg: the practice room (1), Kirill’s home (2), Ilya’s home (3), Pasha’s and Oleg’s home (4; they live close to each other) and his own home (5). Now he has to find the optimal path. The path should start at point 1, end at point 5 and have minimum length. Moreover, Ilya doesn’t want to be the last student to get home, so point 3 can’t be fourth in the path.

Input

The input contains a table of distances between the key points. It has five rows and five columns. The number in the i’th row and the j’th column (1 ≤ ij ≤ 5) is a distance between points i andj. All distances are non-negative integers not exceeding 10 000. It is guaranteed that the distance from any point to itself equals 0, and for any two points, the distance between them is the same in both directions. It is also guaranteed that the distance between any two points doesn’t exceed the total distance between them through another point.

Output

Output two lines. The first line should contain the length of the optimal path. The second line should contain five space-separated integers that are the numbers of the points in the order Misha should visit them. If the problem has several optimal solutions, you may output any of them.

Sample

input output
0 2600 3800 2600 2500
2600 0 5300 3900 4400
3800 5300 0 1900 4500
2600 3900 1900 0 3700
2500 4400 4500 3700 0
13500
1 2 3 4 5


摆明的一个TSP问题。可是解决TSP问题的效率是相当低的。

只是这里的点数非常少。并且有一个条件限制。

所以最后剩下的可选择的路径就非常少了。

于是这里我使用了暴力法,能够非常轻松地通过。

技巧就是:预先产生了路径,那么速度就快了。

#include <vector>
#include <iostream>
using namespace std;

static const int NUM_OF_MEN = 5;

void TaxiforProgrammers2005()
{
	vector<vector<int> > mat(NUM_OF_MEN, vector<int>(NUM_OF_MEN));
	for (int i = 0; i < NUM_OF_MEN; i++)
	{
		for (int j = 0; j < NUM_OF_MEN; j++)
		{
			cin>>mat[i][j];
		}
	}
	int routes[4][5] = { {1,2,3,4,5}, {1,3,2,4,5}, {1,3,4,2,5}, {1,4,3,2,5} };
	int ans = 50000, rou = -1;
	for (int i = 0; i < 4; i++)
	{
		int tmp = 0;
		for (int j = 1; j < 5; j++)
		{
			tmp += mat[routes[i][j-1]-1][routes[i][j]-1];
		}
		if (tmp < ans)
		{
			ans = tmp;
			rou = i;
		}
	}
	cout<<ans<<endl;
	for (int i = 0; i < 5; i++)
	{
		cout<<routes[rou][i]<<' ';
	}
}



转载于:https://www.cnblogs.com/clnchanpin/p/6743626.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值