[bzoj]1027 Alloy (JSOI2007)

Source: Internet
Author: User

Do not know how to evaluate it, very god of a problem, even if it is 10 years ago the topic can not be underestimated ah.

Description

A company processes an alloy consisting of iron, aluminum, and tin. Their work is very simple. First import some Fe-al-SN alloy raw materials, different kinds of raw materials in the proportion of Fe-al-Sn. Then, each raw material is taken out of a certain amount, after melting, mixing, to obtain a new alloy. The new alloy's Fe-al-SN specific gravity is required by the user. Now, the user gives the n kinds of alloys they need, and the proportion of Fe-al-sn in each alloy. The company wants to be able to order a minimum variety of raw materials and use the raw materials to produce all the kinds of alloys the user needs.

Input

The first line is two integers m and n, respectively, indicating the number of raw materials and the number of alloys required by the user. 2nd to M + 1 lines, each row of three real numbers a, B, C, respectively, the proportion of iron and aluminum tin in a raw material accounted for. M + 2 to M + N + 1 lines, three real numbers a, B, c per line, respectively, representing the proportion of Fe-al-Sn in a user-required alloy.

Output

An integer that represents the minimum number of raw material species required. If there is no solution, the output –1.

Sample Input

10 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 Output

5

HINT

M, N≤500,a, B, c≥0 and A + b + c = 1.

Solution

Let's start with a conclusion: V1,V2...VN is n vectors, a1,a2...an is n unknown constant, and a1+a2+...+an= is set to D.

Then the synthetic vector v=a1v1+a2v2+...+anvn must be in the convex hull of the vector dv1,dv2...dvn.

Do not know how to prove that you can start from the case of two vectors yy, small c is not much to explain.

So what do you want to do with the question? Three-dimensional convex bag? Careful thinking is not necessarily a convex hull, because it requires the least number of points.

We find that the vector is actually only two-dimensional, because the third dimension is completely deterministic after two dimensions have been determined.

So there are only two dimensions left, but still can not start from the convex hull, how to do? At this point we need some suffocation.

The topic asks us to find a polygon with the smallest number of points (sides) in M points, and all of the n points are covered.

The edges on this polygon must satisfy all n points on one side of this side.

So looking for this polygon is equivalent to starting from a point, each time only n points are on one side of the side, walk the least number of sides, back to the starting point!

So the question becomes the shortest length ring on the graph! Shortest path problem!!

M only 500, with Floyd on the line, in order to get the ranking you can choose Dijkstra, because the side length only 1 you can even BFS.

Note that the answer is 1 and the answer to 2 is the first to be awarded.

#include <cstdio>#include<cstring>#include<algorithm>#include<cmath>#defineINF 0X3FFFFFFF#defineMN 505#defineEPS 1e-12using namespacestd;structvec{Doublex, y; Friend Vecoperator-(Constvec& A,Constvec& b) {return(VEC) {a.x-b.x,a.y-b.y};} FriendDouble operator/(Constvec& A,Constvec& b) {returna.x*b.y-a.y*b.x;} FriendDoubleAbsConstvec& a) {returna.x*a.x+a.y*a.y;}} A[MN],B[MN];intDIS[MN][MN];intN,m,ans;inlineintRead () {intn=0, f=1;CharC=GetChar ();  while(c<'0'|| C>'9') {if(c=='-') f=-1; C=GetChar ();}  while(c>='0'&& c<='9') {n=n*Ten+c-'0'; C=GetChar ();} returnNF;}BOOLCMP1 (Constvec& A,Constvec& B) {returnA.y<b.y | | A.y==b.y && a.x<b.x;}BOOLCheckConstvec& A,Constvec&B) {VEC AB=b-A;  for(RegisterintI=1; i<=m;++i)if(ab/(b[i]-a) <-eps)return false; return true;}intMain () {registerinti,j,k; DoubleZ; N=read (); m=read ();  for(i=1; i<=n;++i) scanf ("%LF%LF%LF",&a[i].x,&a[i].y,&z);  for(i=1; i<=m;++i) scanf ("%LF%LF%LF",&b[i].x,&b[i].y,&z);  for(i=1; i<=n;++i) { for(j=1; j<=m;++j)if(a[i].x!=b[j].x| | A[I].Y!=B[J].Y) Break; if(j>m)return 0*printf ("1"); } sort (A+1, a+n+1, CMP1);  for(i=1; i<n;++i) for(j=i+1; j<=n;++j) { for(k=1; k<=m;++k) {if(Fabs (B[k]-a[i])/(A[j]-a[i]) >eps) Break; if(A[I].Y!=A[J].Y) {if(b[i].y<a[i].y| | B[I].Y&GT;A[J].Y) Break;} Else{if(b[i].x<a[i].x| | b[i].x>a[j].x) Break;} }            if(k>m)return 0*printf ("2"); } memset (Dis, +,sizeof(DIS));  for(i=1; i<=n;++i) for(j=1; j<=n;++j)if(I!=j&&check (A[i],a[j])) dis[i][j]=1;  for(k=1; k<=n;++k) for(i=1; i<=n;++i) for(j=1; j<=n;++j) Dis[i][j]=min (dis[i][j],dis[i][k]+Dis[k][j]); Ans=dis[0][0];  for(i=1; i<=n;++i)if(dis[i][i]>2) ans=min (ans,dis[i][i]); if(ans==dis[0][0])return 0*printf ("-1"); printf ("%d", ans);}

Last Word

From the computational geometry to the graph theory model, this problem is really rare ah, transformation of this thing without a certain brain hole is really unable to think out.

[bzoj]1027 Alloy (JSOI2007)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.