动态规划算法的java实现

一:动态规划

1)动态规划的向前处理法

java中没有指针,所以邻接表的存储需要转化一中形式,用数组存储邻接表

用三个数组u,v,w存储边,u数组代表起点,v数组代表终点,w代表权值;例如:1-->2 权值为9 存为:u[i]=1,v[i]=2,w[i]=9,如果该边为第一条边则i=1;

 

 1 package dynamicProgramming;
 2 
 3 public class FGRAPH {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         int i;
 8         int first[],next[];
 9         int n=12;
10         int m=21;
11         int[] u={0,1,1,1,1,2,2,2,3,3,4,5,5,6,6,7,7,8,8,9,10,11};
12         int[] v={0,2,3,4,5,6,7,8,6,7,8,7,8,9,10,9,10,10,11,12,12,12};
13         int[] w={0,9,7,3,2,4,2,1,2,7,11,11,8,6,5,4,3,5,6,4,2,5};
14         first=new int[n+2];
15         next=new int[m+1];
16         for(i=1;i<n+2;i++)
17             first[i]=-1;
18         for(i=1;i<m+1;i++){
19             next[i]=first[u[i]];
20             first[u[i]]=i;
21             System.out.println(i+" u,v,w: "+u[i]+" "+v[i]+"  "+w[i]);
22         }
23         //遍历
24         int k;
25         int[] cost=new int[n+1];
26         int[] d=new int[n+1];
27         for(i=1;i<n+1;i++){
28             cost[i]=9999;
29             d[i]=-1;
30         }
31         cost[n]=0;
32         for(i=n-1;i>0;i--){
33             k=first[i];
34             while(k!=-1){
35                 //System.out.println(u[k]+" "+v[k]+" "+w[k]);
36                 if(w[k]+cost[v[k]]<cost[u[k]]){
37                     cost[u[k]]=w[k]+cost[v[k]];
38                     d[u[k]]=v[k];
39                 }
40                 k=next[k];
41             }    
42         }
43         i=1;
44         System.out.println();
45         System.out.print("1");
46         while(d[i]!=-1){
47             System.out.print("->"+d[i]);
48             d[i]=d[d[i]];
49         }
50         System.out.println("    cost[1]:"+cost[1]);
51     }
52 
53 }
View Code

 

2)动态规划的向后处理法

 

 1 package dynamicProgramming;
 2 
 3 public class BGRAPH {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         int i;
 8         int first[],next[];
 9         int n=12;
10         int m=21;
11         int[] u={0,1,1,1,1,2,2,2,3,3,4,5,5,6,6,7,7,8,8,9,10,11};
12         int[] v={0,2,3,4,5,6,7,8,6,7,8,7,8,9,10,9,10,10,11,12,12,12};
13         int[] w={0,9,7,3,2,4,2,1,2,7,11,11,8,6,5,4,3,5,6,4,2,5};
14         first=new int[n+1];
15         next=new int[m+1];
16         for(i=1;i<n+1;i++)
17             first[i]=-1;
18         for(i=1;i<m+1;i++){
19             next[i]=first[u[i]];
20             first[u[i]]=i;
21             System.out.println(i+" u,v,w: "+u[i]+" "+v[i]+"  "+w[i]);
22         }
23         //遍历
24         int k;
25         int[] cost=new int[n+1];
26         int[] d=new int[n+1];
27         for(i=1;i<n+1;i++){
28             cost[i]=9999;
29             d[i]=-1;
30         }
31         cost[1]=0;
32         for(i=1;i<n+1;i++){
33             k=first[i];
34             while(k!=-1){
35                 if(w[k]+cost[u[k]]<cost[v[k]]){
36                     cost[v[k]]=w[k]+cost[u[k]];
37                     d[v[k]]=u[k];
38                 }
39                 k=next[k];
40             }    
41         }
42         i=12;
43         System.out.println();
44         System.out.print("12");
45         while(d[i]!=-1){
46             System.out.print(" <-- "+d[i]);
47             d[i]=d[d[i]];
48         }
49         System.out.println("    cost[1]:"+cost[12]);
50     }
51 
52 }
View Code

 

posted @ 2015-12-08 16:15  YoZane  阅读(371)  评论(0编辑  收藏  举报