盒模型bug的解决方法
作者:阿捷 2004-7-22 22:37:07
//==========================================================================
// version 変更日 変更者 変更内容
//--------------------------------------------------------------------------
// 1.00 2006/01/31 周振興 新規作成
//**************************************************************************
package path;
/**
* @author 周振興
* @version 1.00
*/
public class Edge {
public String StartNodeID;
public String EndNodeID;
public double Weight;
/**
* @return Returns the endNodeID.
*/
public String getEndNodeID() {
return EndNodeID;
}
/**
* @param endNodeID The endNodeID to set.
*/
public void setEndNodeID(String endNodeID) {
EndNodeID = endNodeID;
}
/**
* @return Returns the startNodeID.
*/
public String getStartNodeID() {
return StartNodeID;
}
/**
* @param startNodeID The startNodeID to set.
*/
public void setStartNodeID(String startNodeID) {
StartNodeID = startNodeID;
}
/**
* @return Returns the weight.
*/
public double getWeight() {
return Weight;
}
/**
* @param weight The weight to set.
*/
public void setWeight(double weight) {
Weight = weight;
}
}
//==========================================================================
// version 変更日 変更者 変更内容
//--------------------------------------------------------------------------
// 1.00 2006/01/31 周振興 新規作成
//**************************************************************************
package path;
import java.util.ArrayList;
/**
* @author 周振興
* @version 1.00
*/
public class Node {
private String iD;
private ArrayList edgeList;
public Node(String id) {
this.iD = id;
this.edgeList = new ArrayList();
}
/**
* @return Returns the edgeList.
*/
public ArrayList getEdgeList() {
return edgeList;
}
/**
* @param edgeList The edgeList to set.
*/
public void setEdgeList(ArrayList edgeList) {
this.edgeList = edgeList;
}
/**
* @return Returns the iD.
*/
public String getID() {
return iD;
}
/**
* @param id The iD to set.
*/
public void setID(String id) {
iD = id;
}
}
//==========================================================================
// version 変更日 変更者 変更内容
//--------------------------------------------------------------------------
// 1.00 2006/01/31 周振興 新規作成
//**************************************************************************
package path;
import java.util.ArrayList;
/**
* @author 周振興
* @version 1.00
*/
public class PassedPath {
private String curNodeID;
private boolean beProcessed;
private double weight;
private ArrayList passedIDList;
public PassedPath(String ID) {
this.curNodeID = ID;
this.weight = Double.MAX_VALUE;
this.passedIDList = new ArrayList();
this.beProcessed = false;
}
/**
* @return Returns the beProcessed.
*/
public boolean isBeProcessed() {
return beProcessed;
}
/**
* @param beProcessed The beProcessed to set.
*/
public void setBeProcessed(boolean beProcessed) {
this.beProcessed = beProcessed;
}
/**
* @return Returns the curNodeID.
*/
public String getCurNodeID() {
return curNodeID;
}
/**
* @param curNodeID The curNodeID to set.
*/
public void setCurNodeID(String curNodeID) {
this.curNodeID = curNodeID;
}
/**
* @return Returns the passedIDList.
*/
public ArrayList getPassedIDList() {
return passedIDList;
}
/**
* @param passedIDList The passedIDList to set.
*/
public void setPassedIDList(ArrayList passedIDList) {
this.passedIDList = passedIDList;
}
/**
* @return Returns the weight.
*/
public double getWeight() {
return weight;
}
/**
* @param weight The weight to set.
*/
public void setWeight(double weight) {
this.weight = weight;
}
}
//==========================================================================
// version 変更日 変更者 変更内容
//--------------------------------------------------------------------------
// 1.00 2006/01/31 周振興 新規作成
//**************************************************************************
package path;
import java.util.ArrayList;
import java.util.Hashtable;
/**
* @author 周振興
* @version 1.00
*/
public class PlanCourse {
private Hashtable htPassedPath;
public PlanCourse(ArrayList nodeList, String originID) throws Exception {
this.htPassedPath = new Hashtable();
Node originNode = null;
for (int i = 0; i < nodeList.size(); i++) {
Node node = (Node) nodeList.get(i);
if (node.getID() == originID) {
originNode = node;
} else {
PassedPath pPath = new PassedPath(node.getID());
this.htPassedPath.put(node.getID(), pPath);
}
}
if (originNode == null) {
throw new Exception("The origin node is not exist !");
}
this.InitializeWeight(originNode);
}
private void InitializeWeight(Node originNode) {
if ((originNode.getEdgeList() == null) || (originNode.getEdgeList().size() == 0)) {
return;
}
for (int i = 0; i < originNode.getEdgeList().size(); i++) {
Edge edge = (Edge) originNode.getEdgeList().get(i);
PassedPath pPath = getPassedPath(edge.EndNodeID);
if (pPath == null) {
continue;
}
pPath.getPassedIDList().add(originNode.getID());
pPath.setWeight(edge.getWeight());
}
}
public PassedPath getPassedPath(String nodeID) {
return (PassedPath) this.htPassedPath.get(nodeID);
}
}
//==========================================================================
// version 変更日 変更者 変更内容
//--------------------------------------------------------------------------
// 1.00 2006/01/31 周振興 新規作成
//**************************************************************************
package path;
import java.util.ArrayList;
/**
* @author 周振興
* @version 1.00
*/
public class RoutePlanner {
public RoutePlanner() {
}
public RoutePlanResult Paln(ArrayList nodeList, String originID, String destID) throws Exception {
PlanCourse planCourse = new PlanCourse(nodeList, originID);
Node curNode = this.GetMinWeightRudeNode(planCourse, nodeList, originID);
while (curNode != null) {
PassedPath curPath = planCourse.getPassedPath(curNode.getID());
for (int j = 0; j < curNode.getEdgeList().size(); j++) {
Edge edge = (Edge) curNode.getEdgeList().get(j);
PassedPath targetPath = planCourse.getPassedPath(edge.EndNodeID);
double tempWeight = curPath.getWeight() + edge.getWeight();
if (tempWeight < targetPath.getWeight()) {
targetPath.setWeight(tempWeight);
targetPath.getPassedIDList().clear();
for (int i = 0; i < curPath.getPassedIDList().size(); i++) {
targetPath.getPassedIDList().add(curPath.getPassedIDList().get(i).toString());
}
targetPath.getPassedIDList().add(curNode.getID());
}
}
planCourse.getPassedPath(curNode.getID()).setBeProcessed(true);
curNode = this.GetMinWeightRudeNode(planCourse, nodeList, originID);
}
return this.GetResult(planCourse, destID);
}
private RoutePlanResult GetResult(PlanCourse planCourse, String destID) {
PassedPath pPath = planCourse.getPassedPath(destID);
if (pPath.getWeight() == Integer.MAX_VALUE) {
RoutePlanResult result1 = new RoutePlanResult(null, Integer.MAX_VALUE);
return result1;
}
String[] passedNodeIDs = new String[pPath.getPassedIDList().size()];
for (int i = 0; i < passedNodeIDs.length; i++) {
passedNodeIDs[i] = pPath.getPassedIDList().get(i).toString();
}
RoutePlanResult result = new RoutePlanResult(passedNodeIDs, pPath.getWeight());
return result;
}
private Node GetMinWeightRudeNode(PlanCourse planCourse, ArrayList nodeList, String originID) {
double weight = Double.MAX_VALUE;
Node destNode = null;
for (int i = 0; i < nodeList.size(); i++) {
Node node = (Node) nodeList.get(i);
if (node.getID() == originID) {
continue;
}
PassedPath pPath = planCourse.getPassedPath(node.getID());
if (pPath.isBeProcessed()) {
continue;
}
if (pPath.getWeight() < weight) {
weight = pPath.getWeight();
destNode = node;
}
}
return destNode;
}
}
//==========================================================================
// version 変更日 変更者 変更内容
//--------------------------------------------------------------------------
// 1.00 2006/01/31 周振興 新規作成
//**************************************************************************
package path;
/**
* @author 周振興
* @version 1.00
*/
public class RoutePlanResult {
private String[] passedNodeIDs;
private double weight;
public RoutePlanResult(String[] strings, double d) {
this.passedNodeIDs = strings;
this.weight = d;
}
/**
* @return Returns the passedNodeIDs.
*/
public String[] getPassedNodeIDs() {
return passedNodeIDs;
}
/**
* @param passedNodeIDs The passedNodeIDs to set.
*/
public void setPassedNodeIDs(String[] passedNodeIDs) {
this.passedNodeIDs = passedNodeIDs;
}
/**
* @return Returns the weight.
*/
public double getWeight() {
return weight;
}
/**
* @param weight The weight to set.
*/
public void setWeight(double weight) {
this.weight = weight;
}
}
嫌来无视,把c#代码转换成java代码
呵呵,也不知道对不对,给以后的各位提供一个方便
为什么没有初始化,和获得查询就结果的代码?
请回复qq:13448739
8过,我觉得用arraylist速度比较慢,建议斑竹修改。我准备修改一下哦
不知道是程序的问题,还是我构建的图有问题,我发现如果在图中存在一个或多个闭合的回路,该程序好像会出现错误,不知道笔者是否遇到过,应该如何解决呢?
请问我把楼主的代码录入之后,怎么不出现最短路经的结果呢
谢谢
函数public RoutePlanResult Paln(ArrayList nodeList ,string originID ,string destID) 有错误
应该在语句:PassedPath targetPath = planCourse[edge.EndNodeID] 前面加上
if(edge.EndNodeID==originID )
continue;
public class RoutePlanResult
{
private string[] passedNodeIDs;
private double weight;
public RoutePlanResult(string[] strings, double d)
{
this.passedNodeIDs = strings;
this.weight = d;
}
/**
* @return Returns the passedNodeIDs.
*/
public string[] getPassedNodeIDs()
{
return passedNodeIDs;
}
/**
* @param passedNodeIDs The passedNodeIDs to set.
*/
public void setPassedNodeIDs(string[] passedNodeIDs)
{
this.passedNodeIDs = passedNodeIDs;
}
/**
* @return Returns the weight.
*/
public double getWeight()
{
return weight;
}
/**
* @param weight The weight to set.
*/
public void setWeight(double weight)
{
this.weight = weight;
}
}
缺少一个类,补充如下
public class RoutePlanResult
{
private string[] passedNodeIDs;
private double weight;
public RoutePlanResult(string[] strings, double d)
{
this.passedNodeIDs = strings;
this.weight = d;
}
/**
* @return Returns the passedNodeIDs.
*/
public string[] getPassedNodeIDs()
{
return passedNodeIDs;
}
/**
* @param passedNodeIDs The passedNodeIDs to set.
*/
public void setPassedNodeIDs(string[] passedNodeIDs)
{
this.passedNodeIDs = passedNodeIDs;
}
/**
* @return Returns the weight.
*/
public double getWeight()
{
return weight;
}
/**
* @param weight The weight to set.
*/
public void setWeight(double weight)
{
this.weight = weight;
}
}
楼主,有个专业的问题想请问下,我现在想做个瓶颈路段的交通路径选择的编程,可是无从下手,希望楼主给个建议哈,有程序就更好了=。=