杭电acm--1046
来源:互联网 发布:淘宝直通车首次充值 编辑:程序博客网 时间:2024/06/05 19:24
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.
#include<stdio.h>#include<stdlib.h>#include<iostream>#include<cmath>#include<iomanip>//#define P 3.141592653using namespace std;void main(){int k, n, m;double res;int t = 0;cin >> k;while (k--){cin >> n >> m;if (n % 2 == 0 || m % 2 == 0){cout << "Scenario #" << ++t << ":" << endl;res = m*n;cout << fixed << setprecision(2) << res << endl;cout << endl;}else{cout << "Scenario #" << ++t << ":" << endl;res = m*n + 0.414;cout << fixed << setprecision(2) << res << endl;cout << endl;}}system("pause");}
0 0
- 杭电acm--1046
- 杭电ACM 1003
- 杭电 ACM 2016
- 杭电acm 1010
- 杭电 acm 1045
- 杭电ACM-1010
- 杭电ACM 1172
- 杭电ACM 1174
- 杭电ACM 1175
- 杭电ACM 分类
- 杭电ACM分类
- 杭电acm 1000
- 杭电acm 1001
- 杭电acm 1002
- 杭电acm分类
- 杭电acm分类
- 杭电 ACM 1004
- 杭电 ACM 1007
- iframe父级与iframe子级间访问
- TCP的拥塞控制
- 神经网络&深度学习
- tyvj1055 沙子合并 (区间dp)
- WaitForSingleObject的用法
- 杭电acm--1046
- 自定义进度条和RatingBar评分控件
- Oracle导出表(即DMP文件)的两种方法
- nyoj 南阳理工63小猴子下落
- SGI STL中的construct
- PHP学习笔记(10)—— 浮点数与运算优先级
- 堆排序
- javac和java -cp选项情景总结
- 使用IntentService实现图片的下载