HDU 1046 遍历矩形上所有点最小路径
来源:互联网 发布:vb中chr是什么意思 编辑:程序博客网 时间:2024/06/08 10:42
HDU 1046
Gridland
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5612 Accepted Submission(s): 2555
Problem Description
For years, computer scientists have been trying to find efficient solutions to different computing problems. For some of them efficient algorithms are already available, these are the “easy” problems like sorting, evaluating a polynomial or finding the shortest path in a graph. For the “hard” ones only exponential-time algorithms are known. The traveling-salesman problem belongs to this latter group. Given a set of N towns and roads between these towns, the problem is to compute the shortest path allowing a salesman to visit each of the towns once and only once and return to the starting point.
The president of Gridland has hired you to design a program that calculates the length of the shortest traveling-salesman tour for the towns in the country. In Gridland, there is one town at each of the points of a rectangular grid. Roads run from every town in the directions North, Northwest, West, Southwest, South, Southeast, East, and Northeast, provided that there is a neighbouring town in that direction. The distance between neighbouring towns in directions North–South or East–West is 1 unit. The length of the roads is measured by the Euclidean distance. For example, Figure 7 shows 2 × 3-Gridland, i.e., a rectangular grid of dimensions 2 by 3. In 2 × 3-Gridland, the shortest tour has length 6.
The president of Gridland has hired you to design a program that calculates the length of the shortest traveling-salesman tour for the towns in the country. In Gridland, there is one town at each of the points of a rectangular grid. Roads run from every town in the directions North, Northwest, West, Southwest, South, Southeast, East, and Northeast, provided that there is a neighbouring town in that direction. The distance between neighbouring towns in directions North–South or East–West is 1 unit. The length of the roads is measured by the Euclidean distance. For example, Figure 7 shows 2 × 3-Gridland, i.e., a rectangular grid of dimensions 2 by 3. In 2 × 3-Gridland, the shortest tour has length 6.
Input
The first line contains the number of scenarios.
For each scenario, the grid dimensions m and n will be given as two integer numbers in a single line, separated by a single blank, satisfying 1 < m < 50 and 1 < n < 50.
For each scenario, the grid dimensions m and n will be given as two integer numbers in a single line, separated by a single blank, satisfying 1 < m < 50 and 1 < n < 50.
Output
The output for each scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. In the next line, print the length of the shortest traveling-salesman tour rounded to two decimal digits. The output for every scenario ends with a blank line.
Sample Input
22 22 3
Sample Output
Scenario #1:4.00Scenario #2:6.00
Source
Northwestern Europe 2001
Recommend
We have carefully selected several similar problems for you: 1038 1037 1036 1124 1129
水因为要回到起点,故有奇数个点时要走一次斜线
#include <iostream>#include <cstdio>using namespace std;int main(){ int t; int n,m; double ans; while(cin>>t) { for(int i=1;i<=t;i++) { cin>>n>>m; if(n*m%2==0) ans=n*(double)m; else ans=n*(double)m+0.41; cout<<"Scenario #"<<i<<':'<<endl; printf("%.2lf\n\n",ans); } }}
0 0
- HDU 1046 遍历矩形上所有点最小路径
- 图论之遍历所有点的最小距离
- HDU 5251-矩形面积(点集的最小面积包围矩形)
- hdu 5067 遍历指定点集最小时间
- OpenCV 遍历两点直线上的所有点
- hdu 3861(缩点+最小路径覆盖)
- hdu 3864 强连通缩点+最小路径覆盖
- HDU 3861 Tarjan + 缩点 + 最小路径覆盖
- 遍历所有路径
- hdu 1599 find the mincost route(无向图的最小环:求从一个点遍历所有节点以后回到原点的最短路径)
- 紫书p263 嵌套矩形如何输出所有路径(DAG上的动态规划)
- HDU 1151 Air Raid(DAG上的最小路径覆盖)
- boundingRect 输入点返回最小矩形
- 求点1 到点n 所有路径中 最大值与最小值之差 最小的那个
- POJ 1125 可不可能遍历所有点情况下的最短路径
- hdu-5251(矩形最小覆盖模板)
- hdu 3669 Cross the Wall(给你n个矩形,长宽已知,求用不超过k个大矩形包含所有给定矩形,使得大矩形总面积和最小)
- c#判断点是否在矩形上
- [leetcode]347. Top K Frequent Elements
- 【转】Unity协程(Coroutine)原理深入剖析
- 浅谈Java设计模式之代理模式
- Android Studio新建shape等xml文件
- android锁屏唤醒并解锁屏幕
- HDU 1046 遍历矩形上所有点最小路径
- 文本文件和二进制文件的区别
- d-堆
- Android Studio 简单设置
- mysql Last_SQL_Errno: 1032
- 《C专家编程》:C程序运行时的数据结构(六)
- Advice for Applying Machine Learning(Andrew ng ML)
- redis解决单点故障HA(高可用)主从复制
- FreeRTOS历史版本更新记录