【HDU1572】【旅行商问题 状压DP 数据小全排列暴力】下沙小面的(2)

本文介绍了一种解决旅行商问题的方法,通过枚举状态、上一次遍历的点及接下来遍历的点进行转移,利用阶乘的方式直接计算出最优路径。适用于站点数量较少的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=0,M=0,Z=1e9+7,ms63=1061109567;
int casenum,casei;
int n;
int a[30][30];
bool e[30];
int b[8];
int main()
{
	while(scanf("%d",&n),n)
	{
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)scanf("%d",&a[i][j]);
		}
		MS(e,0);
		int m,x;
		scanf("%d",&m);
		for(int i=0;i<m;i++)scanf("%d",&x),e[x]=1;
		int g=0;b[0]=0;
		for(int i=0;i<30;i++)if(e[i])b[++g]=i;
		int ans=1e9;
		do
		{
			int tmp=0;
			for(int i=1;i<=g;i++)tmp+=a[b[i-1]][b[i]];
			gmin(ans,tmp);
		}while(next_permutation(b+1,b+g+1));
		printf("%d\n",ans);
	}
	return 0;
}
/*
【trick&&吐槽】
keep it simple and stupid

【题意】
有n(30)个站点0base。
我们一次拉n(7)个人,我们设计路线,只经过这n个人的目的地中的点,把这n个人用最短的路程全部送到。
输出最短路程。

【类型】
旅行商问题
状压DP

【分析】
这题一看就是典型的旅行商问题。
枚举状态,枚举上一次遍历的点,枚举接下来遍历的点,然后做转移。
然而数据这么小,直接阶乘搞吧!n!直接AC拉!

【时间复杂度&&优化】
O(目的地数量!)

*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值