zoukankan      html  css  js  c++  java
  • 最优装载—dp

    最优装载—dp

    一 问题描述
    enter description here

    二 问题分析
    enter description here

    三 代码实现

    package dp_Loading;
    
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class bin
    {
        public static void main(String[] args) throws IOException
        {
            int []w={0,20,10,40,30,50,60,70};
            int c=180;
            dp_Loading myDp_Loading=new dp_Loading(w, c);
        }
    }
    class dp_Loading
    {
        int w[];
        int c;
        int cc;
        int m[][];
        int x[];       //迭代路线记录
        public dp_Loading(int w[],	int c) throws IOException
        {
            this.c=c;
            this.cc=c;
            this.w=w;
            this.m=new int [w.length][c+1];
            this.x=new int [w.length];
            Dp_Loading();
            display();
        }
        public void Dp_Loading()
        {
            for(int i=0; i<w.length; i++)
            {
                m[i][0]=0;                     //船的容量为0
            }
            for(int j=0; j<=c; j++)
            {
                m[0][j]=0;                     //没有集装箱可装
            }
            for(int i=1; i<w.length; i++)
            {
                for(int j=1; j<w[i]; j++)
                {
                    m[i][j]=m[i-1][j];
                }
                for(int j=w[i]; j<=c;j++)
                {
                    m[i][j]=Math.max(m[i-1][j], m[i-1][j-w[i]]+w[i]);
                }
            }
            //恢复迭代路径
            for(int i=w.length-1; i>=1; i--) 
            {
                if(m[i][cc]==m[i-1][cc]) //这个集装箱没有装入
                {
                    x[i]=0;	
                }
                else 
                {
                    x[i]=1;
                    cc-=w[i];
                }
            }
        }
        public void display() throws IOException
        {
        	BufferedWriter fout=new BufferedWriter(new FileWriter("out.txt"));
        	fout.write("c="+c);
            fout.newLine();
            fout.write("optw="+m[w.length-1][c]);
            fout.newLine();
            fout.write("m[i][j]:");
            fout.newLine();
            for(int i=0; i<w.length; i++)
            {
                for(int j=0; j<=c; j++)
                {
                    fout.write("	"+m[i][j]);
                }
                fout.newLine();
            }
            fout.write("x[i]:");
            fout.newLine();
            for(int i=1; i<w.length; i++)
            {
                fout.write("	"+x[i]);
                fout.newLine();
            }
            fout.flush();
        }
    }
    
    

    四 运行结果
    enter description here

    五 总结收获

    1. dp 关键在于得到递归方程
    2. 由递归方程分析迭代过程,最后给出循环

    六不足

    1. dp应当多加练习
  • 相关阅读:
    springboot文件上传: 单个文件上传 和 多个文件上传
    Eclipse:很不错的插件-devStyle,将你的eclipse变成idea风格
    springboot项目搭建:结构和入门程序
    POJ 3169 Layout 差分约束系统
    POJ 3723 Conscription 最小生成树
    POJ 3255 Roadblocks 次短路
    UVA 11367 Full Tank? 最短路
    UVA 10269 Adventure of Super Mario 最短路
    UVA 10603 Fill 最短路
    POJ 2431 Expedition 优先队列
  • 原文地址:https://www.cnblogs.com/Howbin/p/9923707.html
Copyright © 2011-2022 走看看