Hiker_H
Hiker1995
采纳率100%
2018-09-18 03:12 阅读 4.3k

with return value 3221225477

#include
#include
#define MAX 2
#include
using namespace std;

int n = 3;
const double dist[3][3]={0,1,2,
1,0,3,
2,3,0};

double shortestPath(vector& path,vector& visited,double currentLength){
if(path.size() == n)
return currentLength + dist[path[0]][path.back()];
double ret = 500;
for(int next=0;next<n;next++){
if(visited[next])
continue;
int here = path.back();
path.push_back(next);
visited[next] = true;
double cand = shortestPath(path,visited,currentLength+dist[here][next]);

          ret = min(ret,cand);
        visited[next] = false;
        path.pop_back(); 
}
return ret;

}

int main()
{

vector path;
vector visited(3,false);
double currentLength = 0;
double a = shortestPath(path,visited,currentLength);
cout<<"最短距离为:"<<a<<endl;
return 0;
}


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    king05800 king05800 2018-09-18 06:42

    vector path; 没有元素,传入shortest()后, 取back()值得到的是个未定义的随机值或者应该直接挂了吧?

    点赞 1 评论 复制链接分享
  • ailaillia ailaillia 2018-09-18 03:21

    毛估,应该有个地方没初始化

    点赞 评论 复制链接分享

相关推荐