| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1731 人关注过本帖
标题:新人求大神纠错这个基于Hamilton回路算法的C语言程序
取消只看楼主 加入收藏
odidio120
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2017-5-8
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
新人求大神纠错这个基于Hamilton回路算法的C语言程序
#include <cstdlib>
#include <iostream>
#include <stdlib.h>
#include <time.h>
 
using namespace std;
 
#define N 11
 
int curLightestWeight = 1000000;
int curDepth = 0;
int curWeight = 0;
int depth;
int curCircle[N],bestCircle[N];
bool used[N];
float p[N][N];
 
void update();
void show();
 
void genGraphic(int maxWeight)
{  int i;
    for(i = 0;i < N;i++)
    {
        for(int j = 0;j < N;j++)
        {
                scanf("%f",&p[i][j]);
        }
    }
    for(i = 0;i < N;i++)
    {
        for(int j = 0;j < N;j++)
        {
            cout<<p[i][j]<<"   ";         
        }         
        cout<<endl;
    }
}
 
void MHC_recursion(int curVertex)
{
    curCircle[curDepth] = curVertex;
 
    curDepth++;
    used[curVertex] = true;
 
    if(curWeight + (depth - curDepth) >= curLightestWeight)
    {
        curDepth--;
        used[curVertex] = false;
        return;
    }        
    else if(curDepth == depth)
    {
        int thisWeight = p[curVertex][0];
        curWeight += thisWeight;
        if(curWeight < curLightestWeight)
        {
            curLightestWeight = curWeight;
            update();
        }
        curDepth--;
        used[curVertex] = false;
        curWeight -= thisWeight;
        return;
    }
    else
    {
        for(int i = 0;i < N;i++)
        {
            if(i == curVertex || used[i] == true)
                continue;
            int thisWeight = p[curVertex][i];
            curWeight += thisWeight;
            MHC_recursion(i);
            curWeight -= thisWeight;         
        }  
    }
    used[curVertex] = false;
    curDepth--;
    return;
}
 
void update()
{
    for(int i = 0;i < N;i++)
        bestCircle[i] = curCircle[i];
}
 
void show()
{
    for(int i = 0;i < N;i++)
        cout<<bestCircle[i]<<"-->";
    cout<<bestCircle[0]<<endl;
    cout<<"The weight of the circle is "<<curLightestWeight<<"."<<endl;
}
 
void init()
{
    curDepth = 0;
    curWeight = 0;
    curLightestWeight = 1000000;
    depth = N;
}
 
int main(int argc, char *argv[])
{   
    cout<<"******************************************\n";
    cout<<"**  边不重复的Hamilton回路  **\n";
    cout<<"**-------------------------**\n";
    cout<<"** 程序提示:              **\n";
    cout<<"1.使用之前,请将图中节点从0开始连续编号.\n";
    cout<<"2.节点个数为11.\n";
    cout<<"3.节点个数上限可在源程序中修改.\n";
    cout<<"**-------------------------**\n";
    cout<<"请输入景点矩阵元素:\n";
    genGraphic(9);
    init();
    MHC_recursion(0);
    show();
    system("PAUSE");
    return EXIT_SUCCESS;
}

搜索更多相关主题的帖子: include update C语言 
2017-05-08 23:40
odidio120
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2017-5-8
收藏
得分:0 
回复 楼主 odidio120
图片附件: 游客没有浏览图片的权限,请 登录注册
编译不通过
2017-05-08 23:42
快速回复:新人求大神纠错这个基于Hamilton回路算法的C语言程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018588 second(s), 12 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved