Timus 2005. Taxi for Programmers 题解
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
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
visit them. If the problem has several optimal solutions, you may output any of them.
Sample
| input | output |
|---|---|
0 2600 3800 2600 2500 |
13500 |
摆明的一个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 题解的更多相关文章
- poj 2226 Muddy Fields (转化成二分图的最小覆盖)
http://poj.org/problem?id=2226 Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- poj 3041 Asteroids (最大匹配最小顶点覆盖——匈牙利模板题)
http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- poj2991 Crane(线段树)
Description ACM has bought a new crane (crane -- jeřáb) . The crane consists of n segments of variou ...
- 【POJ 3169 Layout】
Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 12565Accepted: 6043 Description Like every ...
- 【32.70%】【poj 2492】A Bug's Life
Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 34687 Accepted: 11344 Description Backgr ...
- Timus : 1002. Phone Numbers 题解
把电话号码转换成为词典中能够记忆的的单词的组合,找到最短的组合. 我这道题应用到的知识点: 1 Trie数据结构 2 map的应用 3 动态规划法Word Break的知识 4 递归剪枝法 思路: 1 ...
- Timus 1712. Cipher Grille 题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/.未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- 2005年NOIP普及组复赛题解
题目涉及算法: 陶陶摘苹果:入门题: 校门外的树:简单模拟: 采药:01背包: 循环:模拟.高精度. 陶陶摘苹果 题目链接:https://www.luogu.org/problem/P1046 循环 ...
- 【题解】Berland.Taxi Codeforces 883L 模拟 线段树 堆
Prelude 题目传送门:ヾ(•ω•`)o Solution 按照题意模拟即可. 维护一个优先队列,里面装的是正在运营中的出租车,关键字是乘客的下车时间. 维护一个线段树,第\(i\)个位置表示第\ ...
随机推荐
- SpringBoot+SpringSecurity+Thymeleaf认证失败返回错误信息踩坑记录
Spring boot +Spring Security + Thymeleaf认证失败返回错误信息踩坑记录 步入8102年,现在企业开发追求快速,Springboot以多种优秀特性引领潮流,在众多使 ...
- 射洪中学讲课PPT
咸鱼跑去射洪中学讲课去啦. 高中生好萌呀!!!! 下面是讲课PPT day3 简单吹了一波noip2015如何骗分,所以课件就是去网上到处kuai题解而已,所以课件我就不上传了. day4 题目+题解 ...
- Hihocoder #1082 : 然而沼跃鱼早就看穿了一切 暴力
时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 fjxmlhx每天都在被沼跃鱼刷屏,因此他急切的找到了你希望你写一个程序屏蔽所有句子中的沼跃鱼(“marshtomp”,不区 ...
- JDK环境变量的配置说明
一.Linux下的JDK安装及配置: 1. 使用命令uname –a查看系统的版本确定系统的位数,然后去JDK官网下载相应位数的安装程序,进行安装. 2. 使用rz命令将下载的JDK上传至Linux ...
- PCI DSS合规建设ASV扫描介绍
最近查一些Nessus.Nexpose漏洞扫描工具相关资料,工具介绍都会提到一些审计功能,其中最常见的就是PCI DSS合规性审计.从网上找到一篇介绍较详尽的文章,与大家分享. 原文摘自:http:/ ...
- 11gR2 RAC重新启动后仅仅能起单节点
11gR2 RAC重新启动后仅仅能起单节点 问题背景: 将11gR2 RAC正常部署完毕之后运行两节点重新启动操作发现当中有一个节点的集群资源无法启动,遂再次重新启动该无法启动集群资源的节点,还是不可 ...
- MVC使用Gantt Chart实现甘特图,管理事情进度
借助"甘特图",可以直观地了解任务.活动.工作的进度.dhtmlxGantt是一个开源的Javacirpt库,能帮助我们快速创建"甘特图",本篇体验在MVC中的 ...
- 报错:405 Method Not Allowed
出现错误的原因是:静态文件不能通过post方式访问. 解决办法:改成用get方式访问.
- MVC自定义编辑视图,DateTime类型属性显示jQuery ui的datapicker
实现的效果为:在编辑视图中,对DateTime类型的属性,显示jQuery UI的datepicker.效果如下: Student.cs public class Student { ...
- Android记录20-获取缓存大小和清除缓存功能
Android开发记录20-获取缓存大小和清除缓存功能 转载请注明:IT_xiao小巫 博客地址:http://blog.csdn.net/wwj_748 前言 本篇博客要给大家分享的如何获取应用缓存 ...