当前位置:网站首页>状态压缩DP AcWing 91. 最短Hamilton路径
状态压缩DP AcWing 91. 最短Hamilton路径
2022-07-27 10:35:00 【T_Y_F666】
状态压缩DP AcWing 91. 最短Hamilton路径
原题链接
算法标签
二进制状态 压缩DP
思路

代码
#include<bits/stdc++.h>
#define int long long
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>b;--i)
using namespace std;
const int N = 20, M = 1 << N;
int a[N][N], f[M][N];
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n=read();
rep(i, 0, n){
rep(j, 0, n){
a[i][j]=read();
}
}
memset(f, 0x3f, sizeof f);
// 在初始点尚未走过任何路程, 因此初始值位0
f[1][0]=0;
// i表示所有的情况
rep(i, 0, 1<<n){
rep(j, 0, n){
// j表示走到哪一个点
if(i>>j&1){// j未走过
// k表示走到j这个点之前,以k为终点的最短距离
rep(k, 0, n){
if(i>>k&1){ //k未走过
f[i][j]=min(f[i][j], f[i-(1<<j)][k]+a[k][j]);
}
}
}
}
}
printf("%lld", f[(1<<n)-1][n-1]);
return 0;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- 最长上升子序列模型 AcWing 272. 最长公共上升子序列
- Longest ascending subsequence model acwing 1014. mountaineering
- A deep analysis of the soul of C language -- pointer
- 基于FPGA的ECG信号采集,存储以及传输系统verilog实现
- NFT leaderboard -nft real offer latest address: NFT leaderboard.com
- 洛谷 P3052 [USACO12MAR]Cows in a Skyscraper G
- Openatom openharmony sub forum, see you today at 14:00! Wonderful release of memorabilia attached
- Redis high availability principle
- 2022牛客多校训练(3)A-Ancestor 题目翻译
- Opengauss kernel analysis - statistics and row count estimation
猜你喜欢

Shortest moving distance and entropy of morphological complex

Internal and external troubles of digital collection NFT "boring ape" bayc

Longest ascending subsequence model acwing 1014. mountaineering

Data assets are king. How to analyze the relationship between enterprise digital transformation and data asset management?

如何组装一个注册中心

荒野觅踪---寻找迭代次数

博弈论 AcWing 892. 台阶-Nim游戏

栈 AcWing 3302. 表达式求值

The second method of calculating overlapping integral

SQL Server2000 database error
随机推荐
10 complete half of the questions
洛谷P1441 砝码称重
Take you hand-in-hand to develop a complete classic game [Tetris] from scratch, with less than 200 lines of logic.
推导重叠积分的详细展开式 STO overlap integrals
学习笔记-简易服务器实现
Chunjun supports DDL conversion and automatic execution of heterogeneous data sources - dtmo 02 review (including course playback + courseware)
Use of pyquery
SQL Server2000 database error
Analysis of C language pointer function and function pointer
Today's code farmer girl summarized her notes about NPM package management and URL module
15th largest value of data flow
2022 Niuke multi school training (3) a-ancestor topic translation
Backpack model acwing 1022. Collection of pet elves
properties文件
Budweiser, a well-known beer, plans to launch NFT in an attempt to unveil the "long planned" uplink?
Maximized array sum after 13 K negations
Use of beautifulsoup
What is the issuing price of NFT (Interpretation of NFT and establishment of NFT world outlook)
基于FPGA的ECG信号采集,存储以及传输系统verilog实现
Application of 5g private network in smart medicine