• 旅行售货员(回溯法)


    标题: 旅行售货员
    时 限: 1000 ms
    内存限制: 10000 K
    总时限: 3000 ms
    描述:

    某售货员要到若干城市去推销商品,已知各城市之间的路程(或旅费)。他要选定一条从驻地出发,经过每个城市一遍,最后回到驻地的路线,使总的路程( 或旅费)最小。各个城市之间可能是有向连通的、无向连通的、以及存在某个城市不连通的情况,你的程序应该能够处理所有可能的情况。如下图表示各个城市间无向连通。

    输入:  
    输出:  
    输入样例:  4
    -1 -1 -1 2
    2 -1 -1 -1
    1 3 -1 -1
    -1 -1 1 -1
    输出样例:  8
    提示:  
     
    View Code
     1 import java.util.Scanner;
     2 
     3 public class Main {
     4     static int n = 0; // 图的顶点数
     5     static int[] x = null; // 当前解
     6     static int[] bestx = null; // 当前最优解
     7     static float bestc = 0; // 当前最优值
     8     static float cc = 0; // 当前费用
     9     static float[][] a = null; // 领接矩阵
    10     
    11     public static void backtrack(int i){
    12         if(i==(n-1)){
    13             if(a[x[i-1]][x[i]]!=-1&&
    14                  a[x[i]][x[0]]!=-1&&
    15                  (bestc==-1||cc+a[x[i-1]][x[i]]+a[x[i]][0]<bestc))
    16             {
    17                 for(int j=0;j<n;j++)
    18                     bestx[j]=x[j];
    19                 bestc=cc+a[x[i-1]][x[i]]+a[x[i]][0];
    20             }
    21         }    else{
    22             for(int j=i;j<n;j++){
    23                 if(a[x[i-1]][x[j]]!=-1&&
    24                         (bestc==-1||cc+a[x[i-1]][x[j]]<bestc)){
    25                     Swap(x,i,j);
    26                     cc+=a[x[i-1]][x[i]];
    27                     backtrack(i+1);
    28                     cc-=a[x[i-1]][x[i]];
    29                     Swap(x,i,j);
    30                 }
    31             }
    32         }
    33     }
    34     
    35     public static void Swap(int[] x, int i, int j) {
    36         int temp;
    37         temp = x[i];
    38         x[i] = x[j];
    39         x[j] = temp;
    40     }
    41 
    42     public static void main(String[] args) {
    43         // TODO Auto-generated method
    44         Scanner myscanner = new Scanner(System.in);
    45         n = myscanner.nextInt();
    46 
    47         a = new float[n][n ];
    48         bestx = new int[n ];
    49         x = new int[n];
    50         bestc = -1;
    51 
    52         for (int i = 0; i < n; i++) {
    53             for (int j = 0; j < n; j++) {
    54                 a[i][j] = myscanner.nextFloat();
    55             }
    56         }
    57         for (int i = 0; i < n; i++) {
    58             x[i] = i;
    59         }
    60 
    61         Main.backtrack(1);
    62 
    63         System.out.println((int) bestc);
    64 
    65         /*
    66          * for (int j = 1; j <= n; j++) { System.out.println(bestx[j]); }
    67          */
    68     }
    69 }
  • 相关阅读:
    c# Invalidate() Update() Refresh()的区别
    Winform重画ComboBox背景色
    窗口过程 Wndproc
    设置DatagridView的列头样式
    SQL2005 全文检索超时的解决方案
    winform 获取当前程序运行根目录
    Web API-如何将Controller的返回值转换成HTTP response消息
    ASP.NET Web API 如何通过程序控制返回xml还是json
    C# web api返回类型设置为json的两种方法
    多组 RadioButtonList 获取值
  • 原文地址:https://www.cnblogs.com/xiaofengkang/p/2495209.html
Copyright © 2020-2023  润新知