集合覆盖问题近似计算

5 篇文章 0 订阅

算法作业第三题,集合覆盖问题近似计算

例题:

 

Sample Input

1 2 3 4 5

4

1 2 3

2 4

3 4

4 5

 

Sample Output

2

1 4

只测试了样例一组,可能会存在问题,请谨慎参考

import java.util.*;
public class Main {
	public static ArrayList<Integer> Uaggregate = new ArrayList<Integer>();			//存放全集
	public static ArrayList<Integer> tmpAggregate = new ArrayList<Integer>();		//存放临时的集合
	public static ArrayList<Integer> tmpAgNum = new ArrayList<Integer>();			//存放临时的集合是由哪几个子集组成的
	public static ArrayList<Integer> BestAgNum = new ArrayList<Integer>();			//存放临时的集合是由哪几个子集组成的
	public static int num;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String Str = sc.nextLine();
		String[] str = Str.split(" ");
		for(int i=0;i<str.length;i++) {
			Uaggregate.add(Integer.parseInt(str[i]));			//给全集赋值
		}
		int m = sc.nextInt();
		sc.nextLine();
		num = m;
		ArrayList<Integer> subset[] = new ArrayList[m]; 
		for(int i=0;i<m;i++) {
			Str = sc.nextLine();
			str = Str.split(" ");
			subset[i] = new ArrayList<Integer>();
			for(int j=0;j<str.length;j++) {
				subset[i].add(Integer.parseInt(str[j]));		//子集赋值
			}
		}
		Recursion(subset, 0, m);						//递归遍历全部可能
		System.out.println(num);
		//转化成标准输出格式
		int[] bestAgNum = new int[BestAgNum.size()];				//存放最终最优结果的数组
		for(int i=0;i<BestAgNum.size();i++) {
			System.out.print((BestAgNum.get(i)+1)+" ");
		}
	}
	static void Recursion(ArrayList<Integer> subset[], int i, int m) {
		if(i<m-1)								//不加当前集合
			Recursion(subset, i+1, m);
		ArrayList<Integer> tmp = new ArrayList<Integer>();
		tmp = (ArrayList<Integer>) tmpAggregate.clone();
		tmpAggregate.removeAll(subset[i]);					   //加当前集合
		tmpAggregate.addAll(subset[i]);
		tmpAgNum.add(i);
		if(tmpAggregate.size()==Uaggregate.size() && tmpAgNum.size()<num) {
			BestAgNum = (ArrayList<Integer>) tmpAgNum.clone();
			num = tmpAgNum.size();
		}
		else if(tmpAgNum.size()<num && i<m-1){
			Recursion(subset, i+1, m);
		}
		tmpAggregate = (ArrayList<Integer>) tmp.clone();			//加当前集合情况下退栈
		tmpAgNum.remove(tmpAgNum.size()-1);
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值