Describe
http://www.lydsy.com/JudgeOnline/problem.php?id=1027
A total of three kinds of metal, \ (m\) material, gives the proportion of three kinds of metals in each material.
The three kinds of metal proportion of the n\ alloy are given. Use the material to make the alloy, ask the minimum number of materials required.
Analysis
First, because the ratio of three metals is added to 1, the first two are determined, and the last one is OK, we can use the two-polar coordinates \ ((x, y) \) to represent the first two, so that each material and alloy is a point on the two-dimensional plane.
The next step is to make the alloy with materials.
First, consider using two materials for the alloy, the two materials are \ ((x1,y1) and (X2,y2) \). Then the alloy that is made is \ ((kx1+ (1-k) x2,ky1+ (1-k) y2), 0\leq{k}\leq1\).
Remember two kinds of material is two vector \ (\overrightarrow{a},\overrightarrow{b}\), then the alloy is $ $k \overrightarrow{a}+ (1-k) \overrightarrow{b}$$
equals $ $k \overrightarrow{a}+ (1-k) \overrightarrow{a}+ (1-k) (\overrightarrow{b}-\overrightarrow{a}) $$
That is, the point of the alloy is on a segment of two material points.
So for more than one material, we connect the points of several materials to form a convex polygon, and any point within the polygon can be thought of as a segment (a line connected to a point on the two edges of a polygon), and we make those two points as materials, You can make a point inside the polygon. The point outside the polygon is not on the line segment that can be made of two points.
What if it's a concave polygon? You can remove some of the material into a convex polygon with fewer material types.
So all the alloys are placed inside the polygon (or edge) of the material. We put all the points on the same side of the line with a length of 1 edge, and then the problem is converted to the minimum ring, with Floyd.
Note: In special cases, all alloys are coincident with one material, and the answer is 1. All other things can be generalized.
1#include <bits/stdc++.h>2 using namespacestd;3 4 Const intmaxn= -+5;5 Const Doubleeps=1e-Ten, inf=1<< -;6 intn,m;7 intD[MAXN][MAXN];8InlineintDCMP (Doublex) {9 if(Fabs (x) <eps)return 0;Ten returnX>0?1:-1; One } A structpt{ - Doublex, y; -PtDoublex=0,Doubley=0): X (x), Y (y) {} thePtoperator- (ConstPT &a)Const{returnPT (x-a.x,y-a.y); } - Double operator* (ConstPT &a)Const{returnx*a.y-y*a.x;} - Double operator^ (ConstPT &a)Const{returnx*a.x+y*a.y;} - BOOL operator!= (ConstPT &a)Const{return(dcmp (a.x-x) | | DCMP (a.y-y)); } + }A[MAXN],B[MAXN]; -InlineBOOLSPJ () { + for(intI=1; i<=m;i++){ A BOOLflag=true; at for(intj=1; j<=n;j++)if(a[i]!=B[j]) { -flag=false; - Break; - } - if(flag)return true; - } in return false; - } toInlineBOOLJ (pt p1,pt p2) { + for(intI=1; i<=n;i++)if(dcmp (B[I]-P1) ^ (B[I]-P2)) >0)return false; - return true; the } *InlineintC (pt p1,pt p2) { $ intL=0, r=0;Panax Notoginseng for(intI=1; i<=n;i++){ - Doublet= (P2-P1) * (b[i]-p1); the if(DCMP (t) >0) l++; + Else if(DCMP (T) <0) r++; A if(L&&r)return 0; the } + if(!l&&!r&&j (P1,P2))return 3; - if(l)return 1; $ if(r)return 2; $ return 0; - } -InlinevoidFloyd () { the for(intk=1; k<=m;k++) for(intI=1; i<=m;i++) for(intj=1; j<=m;j++) -D[i][j]=min (d[i][j],d[i][k]+d[k][j]);Wuyi intans=INF; the for(intI=1; i<=m;i++) ans=min (ans,d[i][i]); - if(Ans==inf) {Puts ("-1");return; } Wuprintf"%d\n", ans); - } AboutInlinevoidsolve () { $ if(SPJ ()) {Puts ("1");return; } - for(intI=1; i<=m;i++) for(intj=i+1; j<=m;j++){ - intflag=C (A[i],a[j]); - if(flag==1) d[i][j]=1; A Else if(flag==2) d[j][i]=1; + Else if(flag==3) d[i][j]=d[j][i]=1; the } - Floyd (); $ } theInlinevoidinit () { thescanf"%d%d", &m,&n);DoubleT; the for(intI=0; i<maxn;i++) for(intj=0; j<maxn;j++) d[i][j]=INF; the for(intI=1; i<=m;i++) scanf ("%LF%LF%LF",&a[i].x,&a[i].y,&t); - for(intI=1; i<=n;i++) scanf ("%LF%LF%LF",&b[i].x,&b[i].y,&t); in } the intMain () { the init (); About solve (); the return 0; the}
View Code
1027: [JSOI2007] Alloy time limit:4 Sec Memory limit:162 MB
submit:3382 solved:955
[Submit] [Status] [Discuss] Description
A company processes an alloy consisting of iron, aluminum, and tin. Their work is very simple. First of all, the import of some Fe-al-SN alloy raw materials, different types of
The proportion of Fe, AL and SN in raw materials is different. Then, each raw material is taken out of a certain amount, after melting, mixing, to obtain a new alloy. New alloys of iron and aluminum
The proportion of the tin to the user needs. Now, the user gives the n kinds of alloys they need, and the proportion of Fe-al-sn in each alloy. The company hopes to
To order a minimum variety of raw materials, and use these raw materials to produce all the types of alloys required by the user.
Input
The first line is two integers m and n (M, n≤500), which represent the number of raw materials and the number of alloy species required by the user, respectively. 2nd to M + 1 lines, three per row
A, B, C (A, B, c≥0 and A + b + c = 1), respectively, representing the proportion of Fe-al-Sn in a raw material. Section M + 2 to M +
n + 1 lines, three reals per row, A, B, C (A, B, c≥0 and A + b + c = 1), respectively, in a user-required alloy
The proportion of the total.
Output
An integer that represents the minimum number of raw material species required. If there is no solution, the output –1.
Sample Input10 10
0.1 0.2 0.7
0.2 0.3 0.5
0.3 0.4 0.3
0.4 0.5 0.1
0.5 0.1 0.4
0.6 0.2 0.2
0.7 0.3 0
0.8 0.1 0.1
0.9 0.1 0
1 0 0
0.1 0.2 0.7
0.2 0.3 0.5
0.3 0.4 0.3
0.4 0.5 0.1
0.5 0.1 0.4
0.6 0.2 0.2
0.7 0.3 0
0.8 0.1 0.1
0.9 0.1 0
1 0 0
Sample Output5Hintsource
Bzoj_1027_[jsoi2007]_ Alloy _ (Calculate geometry +floyd to find the smallest ring)