#include<bits/stdc++.h>
using namespace std;
const int N=5; //顶点数
int E[N+1][N+1]; //邻接矩阵
int Q[N+1][N+1]; //加权邻接矩阵
int randnum[]={1,1,1,1,1,1,1,0,0,0};//设置随机数种子
int tot1=0,tot2=0; //用来记录找到的哈密顿回路数
int arr[N+1]; //用来储存通路经过的顶点,称之为盒子
int book[N+1]; //用来表示此数字有没有被用过,初始化为0表示没用过
int min_cost=21*N,min_cost2=21*N; //最小开支
int ti=0;
void f(int step,int now,int cost);
void t(int step,int now,int cost);
int main()
{
//展示该图的基本信息
srand((unsigned)time(NULL));
cout<<"该地图上有"<<N<<"个城市。"<<endl;
for(int i=1;i<=N;i++)
{
for(int j=i+1;j<=N;j++)
{
E[i][j]=E[j][i]=randnum[rand()

这段代码展示了如何生成一个包含5个顶点的随机无向图,并使用邻接矩阵表示。接着,通过深度优先搜索寻找哈密顿回路,并在过程中应用限界剪枝来优化搜索效率。程序还计算了找到的哈密顿回路的最小总成本。最后,输出了搜索过程所用的时间。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



