郊区春游(状压dp,TSP问题)

本文介绍了使用二进制枚举和动态规划(状压DP)解决旅行商问题(TSP)的方法。通过dp[st][i]表示当前状态为st且最后到达点i的最短路径,利用弗洛伊德算法计算节点间的最短距离,进行状态转移,寻找最优解。
摘要由CSDN通过智能技术生成

在这里插入图片描述

思路:我们选几个点走,就用二进制枚举所有可能的情况1表示走过0表示没有走过,我们用dp[st][i]表示当前状态为st且最后到的点i的最短距离,那么可以发现他的当前状态st是由st‘来的st’必定比st少一个1,那么所以符合条件的dp[st][i]=dp[st’][j]+dis[j][i]转移过来,dis是j到i的最短距离,通过弗洛伊德算法求出来即可,然后在找出符合条件的状态进行转移。当st’i位置比st少个1时那么他转移的状态就是st’+(1<<(i-1))==st(目标状态)

#pragma GCC optimize(2)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<iomanip>
#include<cstring>
#include<time.h>
using namespace std;
typedef long long ll;
#define SIS std::ios::sync_with_stdio(false)
#define space putchar(' ')
#define enter putchar('\n')
#define lson root<<1
#define rson root<<1|1
typedef pair<int,int> PII;
const int mod=1e9+7;
const int N=2e6+10;
const int M=1e3+10;
const int inf=0x3f3f3f3f;
const int maxx=2e5+7;
const double eps=1e-6;
int gcd(int a,int b)
{
   
    return b==0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值