求看代码哪里出问题了

kjackan 发布于 2014/06/14 17:50
阅读 110
收藏 0
就是求一个无向图任意两点的最短路径,我用的深度优先遍历,运行的时候就说不能“read”什么的
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
static int count=0;
//求一条路径
void oneway(int **weight,int p1,int p2,string path,int num,int length,int *visit,string *allpath,  int *total){
                      if(p1==p2){
                               path+="->"+p1;
                               allpath[count]=path;
                                total[count]=length;
                                count++;
                                cout<<"Path; "<<path<<" length: "<<length;
                           }
                         else{
                                 path+="->"+p1;
                                  visit[p1]=1;
                                  for(int j=0;j<num;j++)
                                         if(visit[j]==0&&weight[p1][j]!=0){
                                                 oneway(weight,j,p2,path,num,length+weight[p1][j],visit,allpath,total);
                                              }
                                        visit[p1]=0;
                                 }
                  }
//利用上面递归求出所有路径
void allways(int num,int **weight){
                               string *allpath;    //存所有路径
                               int *total;                    //存所有路径的长度
                                total=(int *)malloc(100*sizeof(int));
                               allpath=(string *)malloc(100*sizeof(string));
                                 int p1,p2;        
                                 string path;               //存一条路径
                                cout<<"input two points(p1 and p2): ";
                                cin>>p1>>p2;
                                cout<<endl;
                                 int visit[100];       //判断点是否被访问
                                 for(int i=0;i<num;i++)
                                         visit[i]=0;
                                  visit[p1]=1;
                                   int length=0;
                                   path+=p1;
                                   for(int i=0;i<num;i++)
                                          if(weight[p1][i]!=0){
                                                  length=weight[p1][i];
                                             oneway(weight,i,p2,path,num,length,visit,allpath,total);
                                          }
                                 cout<<endl;
                }
int _tmain(int argc, _TCHAR* argv[])
{
                        int **weight;    //存邻接矩阵
                        weight=new int*[100];
                        for(int i=0;i<100;i++)
                                weight[i]=new int[100];
                              int num;
                              cout<<"input the num of point";
                              cin>>num;
                              cout<<"input the weight between the points";
                               for(int i=0;i<num;i++){
                                         for(int j=0;j<num;j++)
                                                  cin>>weight[i][j];
                                      }
                              allways(num,weight);
                             return 0;
}
加载中
返回顶部
顶部