动态规划算法的java实现
2015-12-08 16:15
411 查看
一:动态规划
View Code
package dynamicProgramming; public class BGRAPH { public static void main(String[] args) { // TODO Auto-generated method stub int i; int first[],next[]; int n=12; int m=21; int[] u={0,1,1,1,1,2,2,2,3,3,4,5,5,6,6,7,7,8,8,9,10,11}; int[] v={0,2,3,4,5,6,7,8,6,7,8,7,8,9,10,9,10,10,11,12,12,12}; int[] w={0,9,7,3,2,4,2,1,2,7,11,11,8,6,5,4,3,5,6,4,2,5}; first=new int[n+1]; next=new int[m+1]; for(i=1;i<n+1;i++) first[i]=-1; for(i=1;i<m+1;i++){ next[i]=first[u[i]]; first[u[i]]=i; System.out.println(i+" u,v,w: "+u[i]+" "+v[i]+" "+w[i]); } //遍历 int k; int[] cost=new int[n+1]; int[] d=new int[n+1]; for(i=1;i<n+1;i++){ cost[i]=9999; d[i]=-1; } cost[1]=0; for(i=1;i<n+1;i++){ k=first[i]; while(k!=-1){ if(w[k]+cost[u[k]]<cost[v[k]]){ cost[v[k]]=w[k]+cost[u[k]]; d[v[k]]=u[k]; } k=next[k]; } } i=12; System.out.println(); System.out.print("12"); while(d[i]!=-1){ System.out.print(" <-- "+d[i]); d[i]=d[d[i]]; } System.out.println(" cost[1]:"+cost[12]); } }
View Code
相关文章推荐
- Java学习笔记-适配器用法
- 【Java基础】正确使用 Volatile 变量
- 【实战Java高并发程序设计 4】数组也能无锁:AtomicIntegerArray
- 简单的甄别32位和64位jdk的方法
- 你多久没有编程了(java),还记得这是哪的配置文件吗?java框架配置文件全解
- java命令行运行报错解决办法
- Spring:Aware和事件
- JAVA double类型数值,通过NumberFormat类取值的尝试
- Spring3 -- IOC容器的基本原理 02
- java 正则表达式--检验日期格式
- Struts按住alt+/没有提示
- java web开发
- java多线程--volatile关键字
- Spring:Spring整合Hibernate
- Java学习----不变的常量
- java中数组的排序
- Spring:事务详解
- Java学习----Java数据类型
- Springmvc中使用redirect:list.do 时无法跳转
- spring 定时任务的配置文件解析