1027: [JSOI2007]合金 - BZOJ

时间:2021-07-23 13:58:08

Description

某公司加工一种由铁、铝、锡组成的合金。他们的工作很简单。首先进口一些铁铝锡合金原材料,不同种类的原材料中铁铝锡的比重不同。然后,将每种原材料取出一定量,经过融解、混合,得到新的合金。新的合金的铁铝锡比重为用户所需要的比重。 现在,用户给出了n种他们需要的合金,以及每种合金中铁铝锡的比重。公司希望能够订购最少种类的原材料,并且使用这些原材料可以加工出用户需要的所有种类的合金。
Input

第一行两个整数m和n(m, n ≤ 500),分别表示原材料种数和用户需要的合金种数。第2到m + 1行,每行三个实数a, b, c(a, b, c ≥ 0 且 a + b + c = 1),分别表示铁铝锡在一种原材料中所占的比重。第m + 2到m + n + 1行,每行三个实数a, b, c(a, b, c ≥ 0 且 a + b + c = 1),分别表示铁铝锡在一种用户需要的合金中所占的比重。
Output

一个整数,表示最少需要的原材料种数。若无解,则输出–1。
Sample Input
3 2
0.25 0.25 0.5
0 0.6 0.5
1 0 0
0.7 0.1 0.2
0.85 0.05 0.1

Sample Output
2

看到这题首先想到向量

虽然它有三维,但是有一维是不需要的(前两维都对了,第三维肯定也对了)

所以我们可以把它们都看成平面上的点

观察和分析后,发现两种合金合成另一种合金的条件是,第三种合金的点在前两种合金的点的连线段上

所以我们选定一些点作为原料,那么在这些点构成的凸包内部的点都能合成

所以我们要选最少的点,使得目标点都在这些点所构成的凸包内

相当于我们要选最少的边,把目标点围起来

枚举两个点,如果目标点都在左边或在线段上,距离就为1,否则距离为inf,这个用叉积判断

然后用floyd求最小环就行了

floyd最小环是不能解决1或2的,所以要打一个特判ans=1或2的

 const
maxn=;
inf=;
eps=1e-7;
type
node=record
x,y:double;
end;
var
n,m,ans:longint;
a,b:array[..maxn]of node;
f,g:array[..maxn,..maxn]of longint;
flag:array[..maxn]of boolean; function cj(x1,y1,x2,y2:double):double;
begin
exit(x1*y2-y1*x2);
end; procedure init;
var
i,j,k,num:longint;
s:double;
begin
ans:=inf;
read(n,m);
fillchar(f,sizeof(f),);
fillchar(g,sizeof(g),);
for i:= to n do
read(a[i].x,a[i].x,a[i].y);
for i:= to m do
read(b[i].x,b[i].x,b[i].y);
for i:= to n do
begin
j:=;
for k:= to m do
if (abs(a[i].x-b[k].x)>eps) or (abs(a[i].y-b[k].y)>eps) then
begin
j:=;
break;
end;
if j= then
begin
write();
halt;
end;
end;
for i:= to n do
for j:= to n do
if i<>j then
begin
g[i,j]:=;
f[i,j]:=;
num:=;
for k:= to m do
begin
s:=cj(a[j].x-a[i].x,a[j].y-a[i].y,b[k].x-a[i].x,b[k].y-a[i].y);
if (abs(s)<eps) and ((b[k].x-a[i].x)*(b[k].x-a[j].x)<) then inc(num);
if s>eps then
begin
g[i,j]:=inf;
f[i,j]:=inf;
break;
end;
end;
if num=m then
begin
write();
halt;
end;
end;
for i:= to n do
for j:= to n do
if f[i,j]<inf then flag[i]:=true;
end; function min(x,y:longint):longint;
begin
if x<y then exit(x);
exit(y);
end; procedure work;
var
i,j,k:longint;
begin
for k:= to n do
if flag[k] then
begin
for i:= to k- do
if flag[i] then
for j:= to i- do
if flag[j] then
ans:=min(ans,min(f[i,j]+g[j,k]+g[k,i],f[j,i]+g[i,k]+g[k,j]));
for i:= to n do
if flag[i] then
for j:= to n do
if flag[j] then
f[i,j]:=min(f[i,j],f[i,k]+f[k,j]);
end;
if ans=inf then write(-)
else write(ans);
end; begin
init;
work;
end.