当前位置:网站首页>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
边栏推荐
- Servlet template engine usage example
- win查看端口占用 命令行
- MySQL / SQL Server判断表或临时表存在则删除
- Close the computer port
- ROP Emporium x86_ 64 7 ~ 8 questions
- The input of El input input box is invalid, and error in data(): "referenceerror: El is not defined“
- Wepy learning record
- 基于多态的职工管理系统源码与一些理解
- Redis data server / database / cache (2022)
- Regular object type conversion tool - Common DOM class
猜你喜欢

Day 3 of learning rhcsa

基于ele封装下拉菜单等组件

ele之Table表格的封装

Leangoo brain map - shared multi person collaborative mind mapping tool

Traversal of l2-006 tree (middle and later order determination binary tree & sequence traversal)

Log cutting - build a remote log collection server

Flink stream processing engine system learning (III)

Interpretation of the future development of smart agriculture

接口请求时间太长,jstack观察锁持有情况

Linux Redis——Redis 数据库缓存服务
随机推荐
php+mysql對下拉框搜索的內容修改
Implementation of distributed scenario business operation log (based on redis lightweight)
Chapter VII project communication management of information system project manager summary
Those years can not do math problems, using pyhon only takes 1 minute?
Classification of technology selection (2022)
期中汇总(概论+应用层+运输层)
Airtrack cracking wireless network password (Dictionary running method)
Typescript Learning Guide
Fashion MNIST dataset classification training
Encapsulate components such as pull-down menu based on ele
Modification du contenu de la recherche dans la boîte déroulante par PHP + MySQL
Mosaic Routing: implement / home / news
Flink learning (XI) watermark
[unity3d] rolling barrage effect in live broadcasting room
How to build an integrated industrial Internet plus hazardous safety production management platform?
Jz76 delete duplicate nodes in linked list
Devil cold rice 𞓜 078 devil answers the market in Shanghai and Nanjing; Communication and guidance; Winning the country and killing and screening; The purpose of making money; Change other people's op
Android high-level interview must ask: overall business and project architecture design and reconstruction
Difference between relative path and absolute path (often asked in interview)
JZ35 replication of complex linked list