Timus 2005. Taxi for Programmers 题解

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]<<‘ ‘;
	}
}

Timus 2005. Taxi for Programmers 题解,码迷,mamicode.com

时间: 2024-10-28 15:19:13

Timus 2005. Taxi for Programmers 题解的相关文章

URAL 2005. Taxi for Programmers (最短路 数学啊)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2005 2005. Taxi for Programmers Time limit: 0.5 second Memory limit: 64 MB The clock shows 11:30 PM. The sports programmers of the institute of maths and computer science have just finished their trai

poj2991 Crane(线段树)

Description ACM has bought a new crane (crane -- je?áb) . The crane consists of n segments of various lengths, connected by flexible joints. The end of the i-th segment is joined to the beginning of the i + 1-th one, for 1 ≤ i < n. The beginning of t

Timus : 1002. Phone Numbers 题解

把电话号码转换成为词典中能够记忆的的单词的组合,找到最短的组合. 我这道题应用到的知识点: 1 Trie数据结构 2 map的应用 3 动态规划法Word Break的知识 4 递归剪枝法 思路: 1 建立Trie字典树.方便查找, 可是字典树不是使用字符来建立的.而是把字符转换成数字.建立一个数字字典树. 然后叶子节点设置一个容器vector<string>装原单词. 2 动态规划建立一个表,记录能够在字典树中找到的字符串的前缀子串 3 假设找到整个串都在字典树中,那么就能够直接返回这个单词

Timus 1104. Don’t Ask Woman about Her Age题解

Mrs Little likes digits most of all. Every year she tries to make the best number of the year. She tries to become more and more intelligent and every year studies a new digit. And the number she makes is written in numeric system which base equals t

Timus 1139. City Blocks 题解

用例点表达进度 识别用例的状态 根据生命周期要求,识别用例的状态及转移. 典型的如瀑布型,一般依次有如下状态:用例识别,用例确认,用例已设计,用例已编码,用例已测试. 采用测试驱动开发(TDD)的一个例子,依次状态:用例识别,已写测试用例,用例已编码,用例已集成,用例已测试. 最简化用例状态,依次状态:用例识别,用例已集成. 从以上例子可以看到,传统生命周期和敏捷方法都可以得到合适的状态转移图. 设定用例状态的完成度 完成度以百分比表示,表示与工作量成正比的完成程度,0%表示刚开始,工作量投入为

【题解】互不侵犯 SCOI 2005 BZOJ 1087 插头dp

以前没学插头dp的时候觉得这题贼难,根本不会做,学了才发现原来是一裸题. 用二进制表示以前的格子的状态,0表示没放国王,1表示放了国王. 假设当前位置为(x,y),需要记录的是(x-1,y-1)至(x,y-1)的信息,共n+1个点. 每个状态有两种决策,第一种是这个格子不放国王,直接转移. 第二种是这个格子放国王,需要满足几个条件才能进行这步转移,条件很显然,具体见代码和注释. 因为状态数很少,所以也用不到BFS转移状态和Hash的技巧,所以这题还是很清真的,细节不多,码量也很小. #inclu

【题解】Berland.Taxi Codeforces 883L 模拟 线段树 堆

Prelude 题目传送门:ヾ(?ω?`)o Solution 按照题意模拟即可. 维护一个优先队列,里面装的是正在运营中的出租车,关键字是乘客的下车时间. 维护一个线段树,第\(i\)个位置表示第\(i\)个房子前面有没有停放出租车,这样在有人需要打车的时候可以快速找到离她最近的车的位置. 对每个房子维护一个堆,里面装的是停在这个房子前面的出租车,关键字是出租车的编号和上一个乘客下车的时间,上一个乘客下车越早,等待时间越长. 然后模拟时间的流逝就可以了,代码非常好写. Code #includ

[题解]洛谷比赛『期末考后的休闲比赛2』

[前言] 这场比赛已经结束了有几天,但我各种忙,虽然AK但还是没来得及写题解.(我才不会告诉你我跑去学数据结构了) T1 区间方差 (就不贴题好了) 首先可以推公式(我们可以知道,线段树然而并不能通过初中学过的方差公式在log(L)内求出方差): (s2表示方差,L表示区间长度,xi表示区间的每一项,最后一个x上画了一根线表示这些数据的平均数) 用二项式定理完全平方公式可得: 再次展开: 另外,再代入以下这个 得到了: 然后继续吧.. 然后duang地一声合并同类项,于是我们得到了: 然后可以高

bzoj 2109 &amp; 2535 航空管制 题解

[] [分析]真的是一道贪心好题.开始我以为是一道大水题.建立拓扑图后(没环就是方便!),直接把最外层设定序号为1,第二层为2,bfs下去即可...结果发现:飞行序号不能相同...于是开始想. 先考虑第一个问题:打印一个合法序列.我开始是这么想的: 观察每个飞机的最晚飞行序号Ki,因为必定有解,所以我们可以让它的序号就是Ki.然后用它的时间去更新前面的时间(图可以反向建立).应该可以维护一个大根堆,每次挑出最大的一个进行处理. [简易代码] memset(T,0x7f,sizeof(T)); f