要求:
下面的程序实现了Dijkstra的算法.
希望能够将各个起始点(A,B,C,D,E,F)到其他各点的最短距离全部显示在struts2的视图上!import java.util.PriorityQueue; 
 import java.util.List; import java.util.ArrayList; import java.util.Collections;
 class Edge
 {
     public final Vertex target;
     public final double weight;
     public Edge(Vertex argTarget, double argWeight)
     { target = argTarget; weight = argWeight; }
 }
 class Vertex implements Comparable<Vertex>
 {
     public final String name;
      public Edge[] adjacencies;
     public double minDistance = Double.POSITIVE_INFINITY;
     public Vertex previous;
     public Vertex(String argName) { name = argName; }
     public String toString() { return name; }
     public int compareTo(Vertex other)
     {
         return Double.compare(minDistance, other.minDistance);
      }
 
 }
 class Dijkstra
 {
     public static void computePaths(Vertex source)
     {
         source.minDistance = 0.;
         PriorityQueue<Vertex> vertexQueue = new PriorityQueue<Vertex>();
         vertexQueue.add(source);
 
         while (!vertexQueue.isEmpty()) {
          Vertex u = vertexQueue.poll();
 
             // Visit each edge exiting u             for (Edge e : u.adjacencies)
             {
                 Vertex v = e.target;
                 double weight = e.weight;
                 double distanceThroughU = u.minDistance + weight;
                 if (distanceThroughU < v.minDistance) {
                  vertexQueue.remove(v);
 
                  v.minDistance = distanceThroughU ;
                  v.previous = u;
                  vertexQueue.add(v);
 
                 }
 
             }
         }
     }
 
 
     public static List<Vertex> getShortestPathTo(Vertex target)
     {
         List<Vertex> path = new ArrayList<Vertex>();
         for (Vertex vertex = target; vertex != null; vertex = vertex.previous)
             path.add(vertex);
 
         Collections.reverse(path);
         return path;
     }
 public static void main(String[] args)
    {

        Vertex v0 = new Vertex("A");
  Vertex v1 = new Vertex("B");
  Vertex v2 = new Vertex("C");
  Vertex v3 = new Vertex("D");
  Vertex v4 = new Vertex("E");
  Vertex v5 = new Vertex("F");
  Vertex v6 = new Vertex("G");
  v0.adjacencies = new Edge[]{ new Edge(v1,  79.83),
                               new Edge(v5,  81.15) };
  v1.adjacencies = new Edge[]{ new Edge(v0,  79.75),
                               new Edge(v2,  39.42),
                               new Edge(v3, 103.00) };
  v2.adjacencies = new Edge[]{ new Edge(v1,  38.65) };
  v3.adjacencies = new Edge[]{ new Edge(v1, 102.53),
                               new Edge(v5,  61.44),
                               new Edge(v6,  96.79) };
  v4.adjacencies = new Edge[]{ new Edge(v5, 133.04) };
  v5.adjacencies = new Edge[]{ new Edge(v0,  81.77),
                               new Edge(v3,  62.05),
                               new Edge(v4, 134.47),
                               new Edge(v6,  91.63) };
  v6.adjacencies = new Edge[]{ new Edge(v3,  97.24),
                               new Edge(v5,  87.94) };
  Vertex[] vertices = { v0, v1, v2, v3, v4, v5, v6 };
  //构造有向图
  Dijkstra hugh = new Dijkstra(); //构造Dijkstra对象
 
  Vertex startPoint;
  startPoint = v0;
 
 
  computePaths(startPoint); //计算起始点为v0的最短路径
  //显示 v0到其他各点的最短路径
    for (Vertex v : vertices)
  {
     if(v == startPoint)
     {
    
     }
     else
     {
      System.out.println("Distance to " + v + ": " + v.minDistance);
      List<Vertex> path = getShortestPathTo(v);
      System.out.println("Path: " + path);   
     }   }
 
}
     
 }

解决方案 »

  1.   

    大侠们帮帮忙啊!小弟对struts2不熟啊
      

  2.   

    楼主这问题比较难理解啊,你是算法有问题还是显示到页面上有问题?还是struts2不会配置?
    你把这个算法贴上来的确吓到我了
      

  3.   

    Distance to B: 79.83
    Path: [A, B]
    Distance to C: 119.25
    Path: [A, B, C]
    Distance to D: 143.2
    Path: [A, F, D]
    Distance to E: 215.62
    Path: [A, F, E]
    Distance to F: 81.15
    Path: [A, F]
    Distance to G: 172.78
    Path: [A, F, G]
    楼主的意识是否是让这个结果在页面上展示?还是说如何利用算法,放入Action?还是?
      

  4.   

    不是算法的问题啊!只要将求得的结果利用struts2的方法显示到页面上!用标签!struts2的配置也教教小弟啊!我只弄过helloworld
      

  5.   

    汗。。 配置action...调用算法。传递结果... 页面<c:foreach/>或者用Struts2中得循环展示  
      

  6.   

    能给个View的代码和model的代码吗?小弟比较笨!
      

  7.   

    Google 4 keywords: struts2 示例
      

  8.   

    struts2 查询实例 另外struts2 标签就够了用了
      

  9.   


    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE struts PUBLIC 
     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
     "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> 
    <!-- 将struts2交给spring管理 -->
       <constant name="struts.objectFactory" value="spring"></constant>
            <!-- 编码 -->
       <!-- <constant name="struts.i18n.encoding"  value="gbk"/> -->
    <!-- 功能维护中 -->
    <package name="repair-struts" extends="struts-default">  
    <action name="请求" class="类所在的路径" method="调用类中的方法">
    <result name="类的返回值">/WEB-INF/jsp/调用方法以后跳转到哪个页面.jsp</result>
    </action> 
    </package>   </struts>
      

  10.   

    晕,有这样的问题。LZ得自己系统地学下strtus等架构才行了
      

  11.   

    我的主要工作内容不再WEB框架!希望哪位大侠能给出视图和Model的修改代码!关键是View的代码这块很不熟!Model只要给个框架就行!
      

  12.   

    Struts配置文件:
    <struts>
      <package name="xz" namespace="/" extends="struts-default">
           <action name="请求名字" method="调用类中的方法" class="请求的路径">
                            <!--  你要跳转的页面 -->
       <result name="xzInputResult">
    /jsp/xinzuo/batchUploadResult.jsp
       </result>
         </action>   
    </package>
    </struts>view层
    <%@ page contentType="text/html;charset=UTF-8"%>
    <%@ include file="/common/taglibs.jsp"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>闯关题录入</title>
    <link type="text/css" href="${webctx}/css/main.css" rel="stylesheet" />
    </head>
    <body>
       <form action="请求的名字,与struts配置文件对应" method="post" name="form" id="form">
    <table width="100%" border="0" cellpadding="0">
       <tr>
    <td height="20" colspan="5">  <span class="size9">星座信息修改</span>
    </td>
     </tr>  <tr>
           <td width="4%" height="20" align="center">
       序号
            </td>
           <td  width="8%" align="center">            日期
           </td>
          <td width="5%" align="center">            星座
          </td>
         <td width="5%" align="center"> 
                类型
         </td>
        <td align="center" bgcolor="#F2F4F6" class="size2">
      内容
        </td>
    </tr>
    <s:iterator id="命名" value="类中保存的list">
    <tr>
      <td align="center" bgcolor="#FFFFFF">
    <s:property value="${命名.属性}" />
     </td>
    <td align="center" bgcolor="#FFFFFF">
         ${命名.属性}
    </td>
    <td align="center" bgcolor="#FFFFFF">
          ${命名.属性}
    </td>
    <td align="center" bgcolor="#FFFFFF">
          ${命名.属性}
    </td>
    <td align="left" bgcolor="#FFFFFF">
          ${命名.属性}
            </td>
    </td>
           </tr>
    </s:iterato>
    </table>
    </form>
    </body>
    </html>