简单的算法题-总收益最大化

题目描述
某公司雇有N名员工,每名员工可以负责多个项目,但一个项目只能交由一名员工负责。现在该公司接到M个项目,令Aij表示第i名员工负责第j个项目所带来的收益,那么如果项目分配得当,总收益最大是多少?
输入
第一行两个整数N和M,1 <= N,M <= 1000
接下来N行,每行包含M个整数,第i行的第j个整数表示Aij
输入
总收益的最大值

题目比较简单,就是对每一个项目,都找到一个最大值,最后累计。

package com.my.data;

import java.util.Scanner;

public class NiuKe {
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int m = in.nextInt();
		int[][] a = new int[n][m];
		for(int i = 0; i < n; i++)
			for(int j = 0; j < m; j++)
				a[i][j] = in.nextInt();
		
		int[] max = new int[m];
		for(int j = 0; j < m; j++){
			max [j] = a[0][j];
			for(int i = 0; i < n; i++)
				if(max[j] < a[i][j])
					max[j] = a[i][j];
		}
		
		int result = 0;
		for(int k = 0; k < m; k++)
			result += max[k];
		System.out.println(result);
		
	}
}

输入

3 3
1 3 3
2 2 2
3 2 1

输出

9
发布了23 篇原创文章 · 获赞 22 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/song_lee/article/details/100107871
今日推荐