zoj 1037 Gridland
来源:互联网 发布:开淘宝怎么样找货源 编辑:程序博客网 时间:2024/05/16 18:12
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
#include <cstdio>#include <cmath>#define sqrt2 sqrt( 2.0 )int main(){ int m, n, t; scanf("%d", &t); for ( int i = 1; i <= t; i++ ){ scanf("%d%d", &m, &n); printf("Scenario #%d:\n", i); if ( m % 2 == 1 && n % 2 == 1 ) printf("%.2lf\n\n", m * 1.0 * n - 1.0 + sqrt2); else printf("%.2lf\n\n", m * 1.0 * n); } return 0;}
- 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
- 二叉树的操作
- 打开本地通知
- 微软推出新版Xbox One手柄 配对更迅速
- Linux 服务器之间时间同步
- 阿里大跌8.78% 蒸发200亿美元
- zoj 1037 Gridland
- 最优比率生成树「Pascal代码」
- 库克表示Apple Watch今年4月份正式出货
- IM聊天进阶(三)IM>openfire>mina>mina搭建服务端和客户端 实现简单点对点聊天
- 价层电子对互斥理论的模拟验证(C++实现)
- js总结
- Sublime Text 2&3中文乱码
- ios 获取全部照片
- 创造纪录!苹果iPhone单季度销量7450万部