当前位置:网站首页>Introduction to ACM [TSP problem]
Introduction to ACM [TSP problem]
2022-04-23 02:56:00 【Hui Xiaoge】
Travel agent problem , namely TSP Problem is one of the famous problems in the field of mathematics .
Suppose there is a traveling salesman to visit n Cities , He has to choose the path to take , The path limit is that each city can only visit once , And finally to return to the original city of departure .
The goal of path selection is to require the path distance to be the minimum of all paths .
This is a very classic question , Generally in ACM Or common in interview algorithm questions .
Here is the shape pressing DP To solve the problem .
Example 1 :

You don't have to come back here , And fixed the starting point and ending point .
f[i][j] It means i State, The end is j Minimum cost of
#include<bits/stdc++.h>
using namespace std;
const int N=25;
int g[N][N],f[1<<21][25],n;
int main(void)
{
cin>>n;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++) cin>>g[i][j];
memset(f,0x3f,sizeof f);
f[1][0]=0;
for(int i=0;i<(1<<n);i++)
{
for(int j=0;j<n;j++)
{
if(i>>j&1)
{
for(int k=0;k<n;k++)
{
int temp=i-(1<<j);
if(temp&1)
{
f[i][j]=min(f[i][j],f[i-(1<<j)][k]+g[k][j]);
}
}
}
}
}
cout<<f[(1<<n)-1][n-1];
return 0;
}
Example 2 :

First floyd(), Then comes the classic model . There is no definite starting point and ending point .
#include<bits/stdc++.h>
using namespace std;
const int N=210;
int g[N][N],f[1<<16][20],a[N];
int n,m,t;
int main(void)
{
cin>>n>>m>>t;
memset(f,0x3f,sizeof f);
memset(g,0x3f,sizeof g);
for(int i=1;i<=n;i++) g[i][i]=0;
for(int i=0;i<t;i++) cin>>a[i];
while(m--)
{
int a,b,c; cin>>a>>b>>c;
g[a][b]=g[b][a]=min(g[a][b],c);
}
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
for(int i=0;i<t;i++) f[1<<i][i]=0;
for(int i=0;i<(1<<t);i++)
{
for(int j=0;j<t;j++)
{
if(i>>j&1)
{
for(int k=0;k<t;k++)
{
if(i>>k&1)
{
f[i][j]=min(f[i][j],f[i-(1<<j)][k]+g[a[k]][a[j]]);
}
}
}
}
}
int ans=1e9;
for(int i=0;i<t;i++) ans=min(ans,f[(1<<t)-1][i]);
cout<<ans;
return 0;
}
版权声明
本文为[Hui Xiaoge]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220657495044.html
边栏推荐
- How can enterprises with major hazard installations ensure the completion of the digital construction task of double prevention mechanism by the end of the year
- [if you want to do a good job, you must first use its tools] Guide for downloading and using paper editing and document management (endnote, latex, jabref, overflow) resources
- Day 4 of learning rhcsa
- Publish to NPM?
- Jz76 delete duplicate nodes in linked list
- JZ76 删除链表中重复的结点
- OCR recognition PDF file
- Chapter VII project communication management of information system project manager summary
- When using art template inheritance, compileerror: invalid or unexpected token generated
- Decision tree principle of machine learning
猜你喜欢

Linux redis - redis ha sentinel cluster construction details & redis master-slave deployment

Implementation of distributed scenario business operation log (based on redis lightweight)
![[unity3d] rolling barrage effect in live broadcasting room](/img/61/46a7d6c4bf887fca8f088e7673cf2f.png)
[unity3d] rolling barrage effect in live broadcasting room

Source code and some understanding of employee management system based on polymorphism

Leangoo brain map - shared multi person collaborative mind mapping tool

Liunx foundation - zabbix5 0 monitoring system installation and deployment

基于Scrum进行创新和管理

Flink learning (XI) watermark

C语言 171. 最近回文数

Huawei machine test question -- deformation of hj53 Yang Hui triangle
随机推荐
Those years can not do math problems, using pyhon only takes 1 minute?
Android 高阶面试必问:全局业务和项目的架构设计与重构
Kubernetes - detailed explanation of pod
Six very 6 computer driver managers: what software is good for driver upgrade? Recommended by the best computer driver management software abroad
Error installing Mongo service 'mongodb server' on win10 failed to start
Résumé du gestionnaire de projet du système d'information Chapitre VI gestion des ressources humaines du projet
The shell monitors the depth of the IBM MQ queue and scans it three times in 10s. When the depth value exceeds 5 for more than two times, the queue name and depth value are output.
基于ele封装下拉菜单等组件
Flink stream processing engine system learning (III)
windows MySQL8 zip安装
《信息系统项目管理师总结》第四章 项目成本管理
JZ22 链表中倒数最后k个结点
Redis data server / database / cache (2022)
L2-006 樹的遍曆(中後序確定二叉樹&層序遍曆)
[if you want to do a good job, you must first use its tools] Guide for downloading and using paper editing and document management (endnote, latex, jabref, overflow) resources
Slave should be able to synchronize with the master in tests/integration/replication-psync. tcl
The way to conquer C language
Chapter V project quality management of information system project manager summary
《信息系統項目管理師總結》第六章 項目人力資源管理
Shell script learning notes -- shell operation on files sed