当前位置: 首页 > article >正文

C/C++,优化算法——双离子推销员问题(Bitonic Travelling Salesman Problem)的计算方法与源代码

1 文本格式


// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;

// Size of the array a[]
const int mxN = 1005;

// Structure to store the x and
// y coordinates of a point
struct Coordinates {
    double x, y;
} a[mxN];

// Declare a 2-D dp array
float dp[mxN][mxN];

// Function to calculate the
// distance between two points
// in a Euclidian plane
float distance(int i, int j)
{
    // Return the distance
    return sqrt(
      (a[i].x - a[j].x) * (a[i].x - a[j].x)
    + (a[i].y - a[j].y) * (a[i].y - a[j].y));
}

// Utility recursive function to find
// the bitonic tour distance
float findTourDistance(int i, int j)
{
    // Memoization
    if (dp[i][j] > 0)
        return dp[i][j];

    // Update dp[i][j]
    dp[i][j] = min(
    findTourDistance(i + 1, j) + distance(i, i + 1),
    findTourDistance(i + 1, i) + distance(j, i + 1));

    return dp[i][j];
}

// Function to find the
// bitonic tour distance
void bitonicTSP(int N)
{
    // Initialize the dp array
    memset(dp, 0, sizeof(dp));

    // Base Case
    for (int j = 1; j < N - 1; j++)
        dp[N - 1][j] = distance(N - 1, N)
              + distance(j, N);

    // Print the answer
    printf("%.2f\n", findTourDistance(1, 1));
}

// Driver Code
int main()
{
    // Given Input
    int N = 3;
    a[1].x = 1, a[1].y = 1;
    a[2].x = 2, a[2].y = 3;
    a[3].x = 3, a[3].y = 1;

    // Function Call
    bitonicTSP(N);
}

2 代码格式


// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;

// Size of the array a[]
const int mxN = 1005;

// Structure to store the x and
// y coordinates of a point
struct Coordinates {
    double x, y;
} a[mxN];

// Declare a 2-D dp array
float dp[mxN][mxN];

// Function to calculate the
// distance between two points
// in a Euclidian plane
float distance(int i, int j)
{
    // Return the distance
    return sqrt(
      (a[i].x - a[j].x) * (a[i].x - a[j].x)
    + (a[i].y - a[j].y) * (a[i].y - a[j].y));
}

// Utility recursive function to find
// the bitonic tour distance
float findTourDistance(int i, int j)
{
    // Memoization
    if (dp[i][j] > 0)
        return dp[i][j];

    // Update dp[i][j]
    dp[i][j] = min(
    findTourDistance(i + 1, j) + distance(i, i + 1),
    findTourDistance(i + 1, i) + distance(j, i + 1));

    return dp[i][j];
}

// Function to find the
// bitonic tour distance
void bitonicTSP(int N)
{
    // Initialize the dp array
    memset(dp, 0, sizeof(dp));

    // Base Case
    for (int j = 1; j < N - 1; j++)
        dp[N - 1][j] = distance(N - 1, N)
              + distance(j, N);

    // Print the answer
    printf("%.2f\n", findTourDistance(1, 1));
}

// Driver Code
int main()
{
    // Given Input
    int N = 3;
    a[1].x = 1, a[1].y = 1;
    a[2].x = 2, a[2].y = 3;
    a[3].x = 3, a[3].y = 1;

    // Function Call
    bitonicTSP(N);
}


http://www.kler.cn/news/163162.html

相关文章:

  • 二分查找|前缀和|滑动窗口|2302:统计得分小于 K 的子数组数目
  • linux常用命令-pip命令详解(超详细)
  • 判断css文字发生了截断,增加悬浮提示
  • 一. 初识数据结构和算法
  • StoneDB-8.0-V2.2.0 企业版正式发布!性能优化,稳定性提升,持续公测中!
  • 十七、FreeRTOS之FreeRTOS事件标志组
  • 麒麟系统进入救援模式或者是crtl D界面排查方法
  • Linux下通过find找文件---通过修改时间查找(-mtime)
  • 网络工程师【目录】
  • Python 潮流周刊#29:Rust 会比 Python 慢?!
  • 初识人工智能,一文读懂人工智能概论(1)
  • win10 笔记本卡顿优化
  • 二叉树的遍历之迭代遍历
  • 文献计量学方法与应用、主题确定、检索与数据采集、VOSviewer可视化绘图、Citespace可视化绘图、R语言文献计量学绘图分析
  • Python嗅探和解析网络数据包
  • 线性回归模型标准公式
  • 解决MySQL字段名与关键字冲突
  • 身份统一管理创新与优化 ——华为云OneAccess应用身份管理服务的2023年
  • cookie总结
  • 什么是自动化测试?什么情况下使用?
  • 【1day】泛微e-office OA系统xml.php 文件 SORT_ID 参数 SQL 注入漏洞学习
  • 计算机基础知识65
  • Linux文件系统与基础IO
  • 【hugging face】bitsandbytes中8 bit量化的理解
  • 在oracle的scn详细说明
  • Kotlin 中密封类、枚举类与密封接口的对比分析
  • Linux——基本指令(一)
  • Nginx按指定格式记录访问日志
  • 联邦多任务蒸馏助力多接入边缘计算下的个性化服务 | TPDS 2023
  • 【LeetCode】28. 找出字符串中第一个匹配项的下标 【字符串单模匹配:KMP算法】