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 题解的更多相关文章

  1. poj 2226 Muddy Fields (转化成二分图的最小覆盖)

    http://poj.org/problem?id=2226 Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

  2. poj 3041 Asteroids (最大匹配最小顶点覆盖——匈牙利模板题)

    http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  3. poj2991 Crane(线段树)

    Description ACM has bought a new crane (crane -- jeřáb) . The crane consists of n segments of variou ...

  4. 【POJ 3169 Layout】

    Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 12565Accepted: 6043 Description Like every ...

  5. 【32.70%】【poj 2492】A Bug's Life

    Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 34687 Accepted: 11344 Description Backgr ...

  6. Timus : 1002. Phone Numbers 题解

    把电话号码转换成为词典中能够记忆的的单词的组合,找到最短的组合. 我这道题应用到的知识点: 1 Trie数据结构 2 map的应用 3 动态规划法Word Break的知识 4 递归剪枝法 思路: 1 ...

  7. Timus 1712. Cipher Grille 题解

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/.未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

  8. 2005年NOIP普及组复赛题解

    题目涉及算法: 陶陶摘苹果:入门题: 校门外的树:简单模拟: 采药:01背包: 循环:模拟.高精度. 陶陶摘苹果 题目链接:https://www.luogu.org/problem/P1046 循环 ...

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

    Prelude 题目传送门:ヾ(•ω•`)o Solution 按照题意模拟即可. 维护一个优先队列,里面装的是正在运营中的出租车,关键字是乘客的下车时间. 维护一个线段树,第\(i\)个位置表示第\ ...

随机推荐

  1. HDU 5698 瞬间移动 数学

    瞬间移动 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5698 Description 有一个无限大的矩形,初始时你在左上角(即第一行第一列),每次 ...

  2. noip 1995 灯的排列问题 排列组合 DFS

    题目描述 设在一排上有N个格子(N≤20),若在格子中放置有不同颜色的灯,每种灯的个数记为N1,N2,……Nk(k表示不同颜色灯的个数). 放灯时要遵守下列规则: ①同一种颜色的灯不能分开: ②不同颜 ...

  3. 使用Chrome快速实现数据的抓取(一)——概述

    对于一些简单的网页,我们可以非常容易的通过Develop Tool来获取其请求报文规律,并仿照其构建报文来获取页面信息.但是,随着网页越来越复杂,许多页面是由js动态渲染生成的.要获取这类信息,则需要 ...

  4. Non-Inverting Level Shifter : +/-5V signal into a 0 to 3.3V

    http://electronicdesign.com/boards/non-inverting-level-shifter-requires-only-one-op-amp-one-supply-v ...

  5. [html5]使用localStorage兼容低版本Safari无法使用indexeddb的情况

    摘要 简单场景描述:将html5开发的app内嵌入ios app中,有部分数据,需要在本地存储,就想到使用浏览器的localstorage或者indexeddb,另外localstorage存储的方式 ...

  6. MFC绘制直方图和饼图

    转载原文: Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE /* Style Definitions */ table.MsoNorma ...

  7. java 内存溢出 栈溢出的原因与排查方法

     1. 内存溢出的原因是什么?        内存溢出是由于没被引用的对象(垃圾)过多造成JVM没有及时回收,造成的内存溢出.如果出现这种现象可行代码排查: 一)是否App中的类中和引用变量过多使用了 ...

  8. Android只播放gif动画

    使用easygifanimator软件把gif动画打散为图片. 第一步:先上图片素材,以下素材放到res/drawable目录下: 转:http://blog.csdn.net/aminfo/arti ...

  9. 解决谷歌google不能访问造成wordpress打开缓慢的问题

    近日谷歌又不能访问了,开始没觉得有什么问题.对于技术控的我一直以来谷歌搜索是我的必备工具,这次似乎和往常不太一样,好几天了还是不能访问,通过修改hosts等办法还是没用,只有FQ出去才能访问,但总不能 ...

  10. Asp.net FileUpload+Image制作头像效果

    在Web开发中会经常使用到个人信息注册,而个人信息中通常需要自己的头像或者照片.今天主要介绍一下使用FileUpload+img控件上传照片. FileUpLoad控件使用介绍 FileUpLoad控 ...