标签:
#ifndef LONG_PATH_H
#define LONG_PATH_H
#include<iostream>
#include<string>
#define MAX 65535
int graphPath_longest(int (*Graph)[5],int Length,int origin,int destin );
void printf_path(int origin,int destin);
#endif
#include"LongPath.h"
#define Max(a,b) a>b? a:b
int pathStore[20];
int graphPath_longest(int (*Graph)[5],int Length,int origin,int destin){
int pathLength=-1;
for(int i=0;i<Length; i++){
if(Graph[origin][i]!=0&&Graph[origin][i]!=MAX&&origin!=destin){
if(graphPath_longest(Graph,Length,i,destin)+Graph[origin][i]>pathLength){
pathLength=graphPath_longest(Graph,Length,i,destin)+Graph[origin][i];
pathStore[origin]=i;
}
}
else if(origin==destin){
pathStore[origin]=destin;
pathLength=Max(0,pathLength);
}
}
return pathLength;
}
void printf_path(int origin,int destin){
int n=origin;
std::cout<<"the longest path in Graph from node "<<origin<<" to node "<<destin<<std::endl;
std::cout<<origin;
while(n!=destin){
n=pathStore[n];
std::cout<<"->"<<n;
}
std::cout<<std::endl;
}
#include "LongPath.h"
int main(){
int Graph[5][5]={MAX};
Graph[0][0]=0;
Graph[0][4]=6;
Graph[1][0]=9;
Graph[1][1]=0;
Graph[1][2]=3;
Graph[2][0]=10;
Graph[2][2]=0;
Graph[2][3]=5;
Graph[3][3]=0;
Graph[3][4]=1;
Graph[4][4]=0;
std::cout<<"The longest path value "<<graphPath_longest(Graph,5,1,4)<<std::endl;
printf_path(1,4);
}
pathLength=graphPath_longest(Graph,Length,i,destin)+Graph[origin][i];
pathStore[origin]=i;
}
std::cout<<origin;
while(n!=destin){
n=pathStore[n];
std::cout<<"->"<<n;
}
上面的代码就是循环输出的。
标签:
原文地址:http://www.cnblogs.com/yml435/p/4655527.html