Java训练work3.Exer5---供应商问题

【问题描述】

有n个某种商品供应商,某地有m个商店,商店需要从供应商那里进货该商品,每个供应商的供应能力有上限,每个商店都有自己的商品需求量(need[i]:表示第i个商店的需求),每个供应商运送单位商品到商店需要一个运费。

【输入形式】

输入说明:第一行包含两个整数N,M

接下来N行包含三个整数,第一个数表示供应商编号,第二数表示供应能力上限,表示该供应商的供应量不能超过这个上限,第三个数表示运送单位商品到商店的运费。

接下来M行包含两个整数,第一个数表示商店编号,第二个数表示某种商品的需求量。

【输出形式】

输出说明:若可以满足所有商店需求,则输出格式如下:每行第一个数表示供应商编号,第二个数为商店编号,第三个数为供应量。

如:1 2 20

表示第1个供应商给第2个商店供应20个单位量的商品

按商店编号顺序,输出所有供应路径(最后一行无换行符)。

若不满足,输出-1

【样例输入】

4 4

0 20 8

1 15 3

2 55 6

3 40 10

0 28

1 36

2 49

3 12

【样例输出】

1 0 15

2 0 13

2 1 36

2 2 6

0 2 20

3 2 23

3 3 12

public class Exer5
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        int M = scan.nextInt();
        Supplier[] supplier = new Supplier[N];
        int sumOfSupplier = 0;
        int sumOfNeed = 0;
        for(int i=0;i<N;i++)
        {
            int index = scan.nextInt();
            int power = scan.nextInt();
            int fee = scan.nextInt();
            supplier[i] = new Supplier(index,power,fee);
            sumOfSupplier+=power;
        }
        Arrays.sort(supplier,new Comparator(){
            @Override
            public int compare(Object o1,Object o2)
            {
                if(o1 instanceof Supplier&& o2 instanceof Supplier)
                {
                    Supplier s1 = (Supplier)o1;
                    Supplier s2 = (Supplier)o2;
                    return s1.fee-s2.fee;
                }
                throw new RuntimeException();
            }
        });
        int[] need = new int[M];
        for(int i=0;i<M;i++)
        {
            scan.nextInt();
            need[i] = scan.nextInt();
            sumOfNeed+=need[i];
        }
        if(sumOfNeed>sumOfSupplier)
        {
            System.out.println(-1);
            System.exit(0);
        }
        int sCount = 0;
        int nCount = 0;
        while(nCount<M)
        {
            Supplier sTemp = supplier[sCount];
            if(sTemp.power<need[nCount])
            {
                need[nCount]-=sTemp.power;
                System.out.println(sTemp.index + " " + nCount + " " + sTemp.power);
                sCount++;
            }
            else if(sTemp.power>need[nCount])
            {
                sTemp.power-=need[nCount];
                System.out.println(sTemp.index + " " + nCount + " " + need[nCount]);
                nCount++;
            }
            else
            {
                System.out.println(sTemp.index + " " + nCount + " " + need[nCount]);
                nCount++;
                sCount++;
            }
        }
        scan.close();
    }
}

class Supplier
{
    int index;
    int power;
    int fee;

    public Supplier(int index,int power, int fee) {
        this.index = index;
        this.power = power;
        this.fee = fee;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值