Aizu - 1634 Balance Scale Contest1814 - 2019年我能变强组队训练赛第七场

Balance Scale

You, an experimental chemist, have a balance scale and a kit of weights for measuring weights of powder chemicals.

For work efficiency, a single use of the balance scale should be enough for measurement of each amount. You can use any number of weights at a time, placing them either on the balance plate opposite to the chemical or on the same plate with the chemical. For example, if you have two weights of 2 and 9 units, you can measure out not only 2 and 9 units of the chemical, but also 11 units by placing both on the plate opposite to the chemical (Fig. C-1 left), and 7 units by placing one of them on the plate with the chemical (Fig. C-1 right). These are the only amounts that can be measured out efficiently.在这里插入图片描述
You have at hand a list of amounts of chemicals to measure today. The weight kit already at hand, however, may not be enough to efficiently measure all the amounts in the measurement list. If not, you can purchase one single new weight to supplement the kit, but, as heavier weights are more expensive, you’d like to do with the lightest possible.

Note that, although weights of arbitrary positive masses are in the market, none with negative masses can be found.

Input
The input consists of at most 100 datasets, each in the following format.

n m
a1 a2 … an
w1 w2 … wm
The first line of a dataset has n and m, the number of amounts in the measurement list and the number of weights in the weight kit at hand, respectively. They are integers separated by a space satisfying 1 ≤ n ≤ 100 and 1 ≤ m ≤ 10.

The next line has the n amounts in the measurement list, a1 through an, separated by spaces. Each of ai is an integer satisfying 1 ≤ ai ≤ 109, and ai ≠ aj holds for i ≠ j.

The third and final line of a dataset has the list of the masses of the m weights at hand, w1 through wm, separated by spaces. Each of wj is an integer, satisfying 1 ≤ wj ≤ 108. Two or more weights may have the same mass.

The end of the input is indicated by a line containing two zeros.

Output
For each dataset, output a single line containing an integer specified as follows.

If all the amounts in the measurement list can be measured out without any additional weights, 0.
If adding one more weight will make all the amounts in the measurement list measurable, the mass of the lightest among such weights. The weight added may be heavier than 108 units.
If adding one more weight is never enough to measure out all the amounts in the measurement list, -1.
Sample Input
4 2
9 2 7 11
2 9
6 2
7 3 6 12 16 9
2 9
5 2
7 3 6 12 17
2 9
7 5
15 21 33 48 51 75 111
36 54 57 93 113
0 0
Output for the Sample Input
0
5
-1
5
题意:给定n个重量,m个砝码重量。问加一个砝码使得能够称出n个重量。问这个砝码的最小重量,可以是0,如果无解则输出-1.

分析:
暴力枚举所有的天平的组合情况,用dfs递归写一遍即可

然后可以这样来想,不是说如果存在一个最小的解,那么这个最小的是不是所有的解的交集呢!!!!!!!!!

这就是这题暴力枚举的思路;这样来做的话,交集会越来越小,这样用set维护的话,第一个就是答案;

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=200;
map<ll,int>ma1;
set<ll>vis_se,inter_se,tem_se;
set<ll>::iterator it;
ll a[N],w[N];
int n,m;
void dfs(int step,ll sum){
    if(step==m+1){
        ll u=abs(sum);
        if(vis_se.find(u)==vis_se.end())
        vis_se.insert(u);
        return ;
    }
    dfs(step+1,sum);
    dfs(step+1,sum+w[step]);
    dfs(step+1,sum-w[step]);
}
int main()
{
//	freopen("in.txt","r",stdin);

    while(scanf("%d%d",&n,&m)!=EOF,m+n){
        vis_se.clear();
        for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
        for(int i=1;i<=m;i++) scanf("%lld",&w[i]);
        dfs(1,0);

        int flag=0;
        for(int i=1;i<=n;i++){
            if(vis_se.find(a[i])!=vis_se.end())
                continue;
            flag++;
            tem_se.clear();
            if(flag==1){
                inter_se.clear();
                for(it=vis_se.begin();it!=vis_se.end();it++){
                    int x1=abs(a[i]-*it);
                    int x2=abs(a[i]+*it);
                    if(tem_se.count(x1)==0)
                        tem_se.insert(x1);
                    if(tem_se.count(x2)==0)
                        tem_se.insert(x2);
                }
                inter_se.clear();
                inter_se=tem_se;
            }else{
                tem_se.clear();
               for(it=vis_se.begin();it!=vis_se.end();it++){
                 int x1=abs(a[i]-*it);
                 int x2=abs(a[i]+*it);
                 if(inter_se.find(x1)!=inter_se.end())
                    tem_se.insert(x1);
                 if(inter_se.find(x2)!=inter_se.end())
                    tem_se.insert(x2);
               }
            inter_se.clear();
            if(tem_se.empty()) break;
                inter_se=tem_se;
            }

        }

        if(flag==0)
            printf("0\n");
        else if(!inter_se.empty())
            printf("%lld\n",*inter_se.begin());
        else
            printf("-1\n");
    }
	return 0;
}
发布了229 篇原创文章 · 获赞 17 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/c___c18/article/details/100046319