我用递归法写了个动态规划法解决旅行商问题的程序,最短路径长度能算出来,可是路径确始终去不出来,求求哪位帮帮忙。
class minPath
{
public minPath()
{
}
double n=0,path=0;
string[] pp = new string[1000];
static int count = 0;
int f = 0;
pathRecord my = new pathRecord();
mycs.DB mydb = new DB();
static string[] node = new string[100];
int pcount;
string[] ss = new string[100];
public string computeLength(string start, string end)
{
SqlCommand pathCmd = new SqlCommand("select distance from nodeDistance where startNode='"+start+"' and endNode='"+end+"'");
DataSet pathSet = mydb.FillDataSet(pathCmd, "nodeDistance");
if (pathSet.Tables["nodeDistance"].Rows.Count != 0)
{
return pathSet.Tables["nodeDistance"].Rows[0][0].ToString();
}
else
{
return "10000";
}
} public pathRecord computeMin(string start, string end, string[] s,ArrayList retArray)//start是起始点,end是终点,s是要途径节点的集合。旅行商问题中start=end
{
double[] pr = new double[s.Length];
count = 1;
if (s.Length == 0)
{
string p = computeLength(start, end);
my.pathLengh = double.Parse(p);
}
else
{
for (int i = 0; i < s.Length; i++)
{
string[] newS = new string[s.Length - 1];
for (int m = 0, j = 0; j < s.Length; j++)
{
if (j != i)
{
newS[m] = s[j];
m++;
}
}
if (s[i] != null & end != null)
{
pr[i] = computeMin(start, s[i], newS,retArray).pathLengh + double.Parse(computeLength(s[i], end));
} }
}
if(pr.Length!=0)
{
my.pathLengh = pr[0];
}
if (s.Length != 0)
{
my.pathid = s[0];
}
for (int k = 0; k < pr.Length; k++)
{
if (pr[k] < my.pathLengh)
{
my.pathLengh = pr[k];
my.pathid = s[k];
// retArray.Add(ss[k].ToString()); }
}
return my;
}
}
class pathRecord
{
public double pathLengh;
public string pathid;
}
class minPath
{
public minPath()
{
}
double n=0,path=0;
string[] pp = new string[1000];
static int count = 0;
int f = 0;
pathRecord my = new pathRecord();
mycs.DB mydb = new DB();
static string[] node = new string[100];
int pcount;
string[] ss = new string[100];
public string computeLength(string start, string end)
{
SqlCommand pathCmd = new SqlCommand("select distance from nodeDistance where startNode='"+start+"' and endNode='"+end+"'");
DataSet pathSet = mydb.FillDataSet(pathCmd, "nodeDistance");
if (pathSet.Tables["nodeDistance"].Rows.Count != 0)
{
return pathSet.Tables["nodeDistance"].Rows[0][0].ToString();
}
else
{
return "10000";
}
} public pathRecord computeMin(string start, string end, string[] s,ArrayList retArray)//start是起始点,end是终点,s是要途径节点的集合。旅行商问题中start=end
{
double[] pr = new double[s.Length];
count = 1;
if (s.Length == 0)
{
string p = computeLength(start, end);
my.pathLengh = double.Parse(p);
}
else
{
for (int i = 0; i < s.Length; i++)
{
string[] newS = new string[s.Length - 1];
for (int m = 0, j = 0; j < s.Length; j++)
{
if (j != i)
{
newS[m] = s[j];
m++;
}
}
if (s[i] != null & end != null)
{
pr[i] = computeMin(start, s[i], newS,retArray).pathLengh + double.Parse(computeLength(s[i], end));
} }
}
if(pr.Length!=0)
{
my.pathLengh = pr[0];
}
if (s.Length != 0)
{
my.pathid = s[0];
}
for (int k = 0; k < pr.Length; k++)
{
if (pr[k] < my.pathLengh)
{
my.pathLengh = pr[k];
my.pathid = s[k];
// retArray.Add(ss[k].ToString()); }
}
return my;
}
}
class pathRecord
{
public double pathLengh;
public string pathid;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货