ZOJ 1037 Gridland
来源:互联网 发布:linux mysql命令 编辑:程序博客网 时间:2024/06/03 13:15
Background
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.
Problem
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.
Figure 7: A traveling-salesman tour in 2 * 3-Gridland.
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.
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
2
2 2
2 3
Sample Output
Scenario #1:
4.00
Scenario #2:
6.00
Source: Northwestern Europe 2001
<span style="font-size:10px;">#include<iostream>#include<iomanip>#include<math.h>using namespace std;int main(){int N;int m,n;double num;while(cin>>N){for(int i=1;i<=N;i++){cin>>m>>n;if(m%2==0||n%2==0)num=n*m;elsenum=n*m+sqrt(2)-1;cout<<"Scenario #"<<i<<":"<<endl;cout<<fixed<<setprecision(2)<<num<<endl;cout<<endl;}}}</span>
- ZOJ-1037-Gridland
- zoj 1037 Gridland
- zoj 1037 Gridland
- zoj 1037 gridland
- ZOJ 1037 Gridland
- ZOJ 1037 Gridland
- zoj 1037 Gridland
- ZOJ 1037 Gridland
- ZOJ 1037 Gridland
- ZOJ 1037 Gridland
- ZOJ - 1037 Gridland
- zoj 1037 Gridland
- ZOJ 1037 Gridland
- zoj 1037 Gridland
- ZOJ 1037 Gridland
- ZOJ 1037 A - Gridland
- zoj 1037 Gridland
- zoj 1037 Gridland
- Hadoop卸载或增加节点
- Unity3d中四元数的使用
- 俺自己的网站上线啦,求围观
- java Cookie实战教程之留下的痕迹
- 是时候写篇博客了,关于linux和http
- ZOJ 1037 Gridland
- 我自己做的网站终于上线啦,求围观www.yustock.live
- 完全素数
- Java、Scala、Go以相同方式实现快速排序代码量比较
- 错误处理集锦
- Unity3d向web服务器发送数据
- OLE DB访问接口“MICROSOFT.JET.OLEDB.4.0”配置为在单线程单位模式下运行,所以该访问接口无法用于分布式
- 牛顿迭代法与一道经典编程问题
- 1.获得项目中info.plist文件的内容 2.沙盒的数据存储及读取 3.控制器view的高度和状态栏statusBar的关系 4.[UIScreen mainScreen].application