标题:新人求大神纠错这个基于Hamilton回路算法的C语言程序
只看楼主
odidio120
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2017-5-8
结帖率:0
已结贴  问题点数:20 回复次数:2 
新人求大神纠错这个基于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
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:20 
你的源文件名是"1.c",而代码语法用的又是C++,你得先说清楚你这是C还是C++
另外,编译失败贴编译错误信息
2017-05-09 08:23



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-476829-1-1.html




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

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