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.
当且仅当m,n均为奇数时,有两个城市之间需要用一条斜线连接,其他均可以用直线连接;
题目在输出时还需注意的几个问题:
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 <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a,m,n,i=1;
double sum;
cin>>a;
while(a--) {
cin>>m>>n;
sum=m*n;
if(m%2==1 && n%2==1)//输入的m,n均为奇数时,斜线比直线长sqrt(2)-1;
sum+=0.41;
/*
if(m%2==1 && n%2==1) {
sum=m*n+0.41;
} else {
sum=m*n;
} */
cout<<"Scenario #"<<i++<<":"<<endl;
cout<<setiosflags(ios::fixed)<<setprecision(2) <<sum<<endl;//c++中用于控制输出结果精度;
cout<<endl;
}
}
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1037