1046

摘要:
50和1<使用namespacestd;intb){returna<inta[2];cin>cas=0;而(n--){cin&gt)排序(a;
Gridland

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4127    Accepted Submission(s): 1900


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. 


 

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<iostream>
#include<stdio.h>
#include<cmath>
#include<algorithm>
using namespace std;
int cmp(int a,int b)
{
    return a<b;
    
}
int main()
{
    int i,j,m,n,cas;
    float len;
    int a[2];
    cin>>n;
    cas=0;
    while(n--)
    {
        cin>>a[0]>>a[1];
        sort(a,a+2,cmp);
        if(a[0]==1)
        len=a[1]-1;
        if(a[0]==2)
        len=a[0]*a[1];
        else
        {
            if((a[0]*a[1])%2==0)
            len=a[0]*a[1];
            else
            len=a[0]*a[1]-1+sqrt(2);
            
        }
        
    
        cas++;
        printf("Scenario #%d:
",cas);
        if(n>=0)
        {
        printf("%.2f
",len);
        cout<<endl;
        }
        //else
        //printf("%.2f
",len);
     
        
        
    }
    return 0;
}

免责声明:文章转载自《1046》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Android 自定义控件(一)WebDeploy有Bug,请勿使用下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

ag-grid实时监测复选框变化

监测复选框变化  1.最终效果:     点击复选框,页面显示已选中多少条          2. 实现步骤:     1. 在ag-grid中添加: @selection-changed="onSelectionChanged" <ag-grid-vue class="table ag-theme-b...

Spring 注解(二)注解工具类

本文转载自Spring 注解(二)注解工具类 导语 首先回顾一下 AnnotationUtils 和 AnnotatedElementUtils 这两个注解工具类的用法: @Test @GetMapping(value = "/GetMapping", consumes = MediaType.APPLICATION_JSON_VALUE) public...

文档兼容性定义,使ie按指定的版本解析

   作为开发人员,特别是作为Web的前端开发人员 ,最悲催的莫过于要不断的,不断的去调试各种浏览器的显示效果,而这其中最让人头痛的莫过于MS下的IE系列浏览器,在IE系列中的调试我们将会发现没有一个是好伺候的,于是不得不学习各种Hack技术来满足各种浏览器之间的兼容。在这种痛苦中不断的挣扎,MS可能也实在是看不下去了,于是在IE8开始,微软引入了文档兼...

My97日期控件My97DatePicker使用(备忘)

前言: 最近老是找不到资料,痛之又痛的情况下,决定好好将所有涉及到的东西通通做个备忘记录。 参考网址: 1、http://www.my97.net/dp/demo/index.htm 2、http://www.mysuc.com/test/My97DatePicker/#m246 使用备忘—— 日期:2010-10-30 版本:4.7 功能:两个日期文本框...

SwipeRefreshLayout 配合fragment 下拉刷新的使用,超级简单

前台demo <android.support.v4.widget.SwipeRefreshLayout android:layout_width="match_parent" android:id="@+id/swipe_container" android:layout_height="wrap_content" > <ListVie...

JSP,servlet和数据库之间传值出现乱码的问题

 近期困扰我非常久的一个问题最终攻克了,为他我头疼了好几天,问题是JSP通过servlet向数据库传值,查询显示在页面的时候出现了乱码,原先我数据库中有两行带有中文的数据,查询的时候倒是没有出现乱码。我debug一下。发现JSP和servlet中全部接受中文字符集的变量都没有出现乱码,我去数据库查看。全部加入的中文字符都是问号,问题发现了,我就百度什...