zoukankan      html  css  js  c++  java
  • (Java实现) 工作分配问题

    工作分配问题
    时间限制: 1 Sec 内存限制: 128 MB
    [提交][状态][讨论版]
    题目描述
    设有n件工作分配给n个人。为第i个人分配工作j所需的费用为c[i][j] 。试设计一个算法,计算最佳工作分配方案,为每一个人都分配1 件不同的工作,并使总费用达到最小。
    输入
    第一行一个正整数n(1<=n<=20),接下来的n 行,每行n 个数,表示工作费用 。
    输出
    输出有m行,每行输出最小总费用。
    样例输入
    5
    50 43 1 58 60
    87 22 5 62 71
    62 98 97 27 38
    56 57 96 73 71
    92 36 43 27 95
    样例输出
    144

    import java.util.Scanner;
    
    
    public class gongzuofenpeiwenti {
    	public static int sum = 0,n;
    	public static int [] [] num ;
    	public static boolean []  bool;
    	public static int min = Integer.MAX_VALUE;
    	public static void main(String[] args) {
    		Scanner sc =new Scanner(System.in);
    		 n = sc.nextInt();
    		num = new int [n+1][n+1];
    		bool = new boolean [n+1];
    		for (int i = 1; i <=n; i++) {
    			for (int j = 1; j <=n; j++) {
    				num[i][j]=sc.nextInt();
    			}
    		}
    		f(1);
    		System.out.println(min);
    	}
    	public static void f(int a){
    		if(a==n+1){
    			if(sum<min){
    				min=sum;
    			}
    			return;
    		}
    		for (int i = 1; i <=n; i++) {
    			if(!bool[i]){
    				sum+=num[a][i];
    				bool[i]=true;
    				f(a+1);
    				sum-=num[a][i];
    				bool[i]=false;
    			}
    		}
    	}
    	
    
    }
    
    
  • 相关阅读:
    Java多线程(3) Volatile的实现原理
    Java 多线程(2)-Executor
    Java 多线程(1)-Thread和Runnable
    nginx+php部署
    MySQL的慢查询分析
    MySQL 错误
    log4j.properties配置详解
    Windows下Nginx的安装与配置(转)
    Java 字符的验证
    Spring MVC3返回JSON数据中文乱码问题解决(转)
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13079052.html
Copyright © 2011-2022 走看看