卜若的代码笔记-算法系列-第5个算法案例分析:Goods Transportation

1.这道题的意思是

我有n个供货的仓库

有m个需要供货的商店

现在需要你设计一个算法,统计里面最多的运输次数和最小的费用

其中费用为n(1,2,3,4...n)和m(1,2,3,...,m)的运输两边的仓库和商店对应的索引的和值

2.原始题目

Goods Transportation

 

Description

The transportation problem is to minimize the cost of transporting good from m source nodes to n destination nodes through arcs. Arcs are directed routes from source to destination which have no capacity limitation, but there is a cost associated with each arc for each unit of goods transported through. Each source has a supply amount, and each destination has a demand amount.

In this problem, the source nodes are numbered from 1 to m, and the destination nodes are numbered from 1 to n. There is one arc between each pair of the source node and the destination node. The cost of the arc originated from source node a (1 <= a <= m) to destination node b (1 <= b <= n) is a + b. The supply for each source nodes and the demand for each destination node are also given. You are going to calculate the maximal possible amount of goods that can be transported from source to destination, and also the minimized cost to achieve that goal.

运输问题是最小化通过弧段将货物从m个源节点运输到n个目标节点的成本。弧段是从源头到目的地的直接路线,没有容量限制,但是每一个弧段所运送的每一件货物都有相应的成本。每个源有一个供应量,每个目的地有一个需求量。

在这个问题中,源节点的编号是从1到m,目标节点的编号是从1到n,源节点和目标节点之间每一对都有一条弧。从源节点a (1 <= a <= m)到目标节点b (1 <= b <= n)的弧成本为a + b,给出每个源节点的供给和每个目标节点的需求。您将计算可以从来源运输到目的地的最大可能的货物数量,以及实现该目标的最小成本。

 

Input

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 10) which is the number of test cases. T test cases follow, each preceded by a single blank line.

For each case, the numbers of source and destination nodes, m (1 <= m <= 10,000) and n (1 <= n <= 10,000), are given in the first line. The next line contains m integers, which are the supply amounts for each source, and the next line n integers, which are the demand amounts of each destination. All supply and demand amounts are non-negative numbers less than or equal to 10,000.

Output

Results should be directed to standard output. Start each case with "Case #:" on a single line, where # is the case number starting from 1. Two consecutive cases should be separated by a single blank line. No blank line should be produced after the last test case.

For each test case print in one line the two integers - the maximal amount of goods that can be transported to destination, and the minimized cost for transporting those goods. Separate them with a single space.

Sample Input

1

3(数据源,m个源节点) 4(目的节点到n个目的节点)
2 5 6(有多少份货物)
4 3 1 5(需要多少份货物)

 3.解题思路

最多的运输次数显然和需求和供应有关 = max(需求数量,供应数量)

其中最小的成本则可以通过不断满足成本最小的需求商店,直到仓库告罄或者商店完全满足。

代码:

package com.company;

import java.util.Stack;

public class TRANS {


    public Stack entrepStack;
    public int[] entrepList;
    public int[] shopList;
    public int sum;
    public int theMaxTrans;

    public  void  init(int[] entrepList,int[] shopList){
        this.entrepList = entrepList;
        this.shopList = shopList;
        entrepStack = new Stack();

        for (int i = entrepList.length-1;i>=0;i--){

            for (int j =0;j<entrepList[i];j++){

                entrepStack.push(i+1);
            }

        }

    }

    public int getMaxTransTimes(){

      
        int sum1 = 0;
        int sum2 = 0;
        for (int i =0;i<entrepList.length;i++){
            sum1+= entrepList[i];

        }
        for (int i = 0;i<shopList.length;i++){
            sum2 += shopList[i];
        }
        if (sum1>sum2){

            this.theMaxTrans = sum1;

        }

        this.theMaxTrans = sum2;
        return  this.theMaxTrans;

    }
    public int statistic(){

        for (int i =0;i<shopList.length;i++){

            for (int j =0;j<shopList[i];j++){

                sum += ((int)this.entrepStack.pop()+i+1);

                if (this.entrepStack.isEmpty()){


                    return sum;
                }


            }

        }
        return sum;

    }

    public static void main(String[] arg){

        TRANS trans = new TRANS();


        int[] initData = new int[]{2,5,6};
        int[] shopData = new int[]{4,3,1,5};
        trans.init(initData,shopData);
        int sum = trans.statistic();
        int times = trans.getMaxTransTimes();
         System.out.println(times+" "+sum);

    }
}

class TransPoint{
    int entrepotIndex;


    public TransPoint(int entrepotIndex){

        entrepotIndex = entrepotIndex;

    }


}
发布了202 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_37080133/article/details/103542696