【BZOJ】1027: [JSOI2007]合金(凸包+floyd)

來源:互聯網
上載者:User

標籤:

http://www.lydsy.com/JudgeOnline/problem.php?id=1027

沒special judge wa了一發好蛋疼...

首先這題大概和黑書上的差不多...由於知道任意兩種材料就能得到第三種材料的含量,所以可以忽略第三種含量...

首先來看,如果有兩種材料,那麼能合成的材料一定在這個線段上,證明很簡單...

假設材料A和B,要合成材料C,將他們材料的含量放到二維座標繫上(x軸為材料1,y軸為材料2)

假設用了a的A,則用了1-a的B,有

$$
\begin{align}
aX_A+(1-a)X_B=X_C \\
aY_A+(1-a)Y_B=Y_C \\
0<=a<=1 \\
\end{align}
$$

顯然了吧...

然後考慮多個點,因為兩兩之間的點都是取得到的,那麼這些兩兩之間的點又能串連起來,重複下去,顯然最終可以取到的點是這個點集的凸包內的點..

//upd:wiki了一下...這個就是凸包的線性意義...

//即

$$S=\{\sum_{x_i \in X} t_ix_i | t_i \in [0, 1] \} $$

因此容易判斷-1的情況(特判一個點取完所有和兩個點的線段取完所有,要不然後邊凸包會錯...)

然後求最少的點形成凸包,很容易想到吧...我在課堂上也yy出來了...

凸包上的點如果所有該包圍的點都在左邊(右手方向),那麼這兩個點是可以串連與右手方向的剩餘原凸包上的點形成凸包的...證明很簡單...凸包上任意的點的子集都是凸包(當然空集除外..)...

然後我們對這些點對連邊...找最小長度的環即可....如此弱的資料隨便來floyd...

#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <iostream>#include <algorithm>#include <queue>#include <set>#include <map>using namespace std;typedef long long ll;#define rep(i, n) for(int i=0; i<(n); ++i)#define for1(i,a,n) for(int i=(a);i<=(n);++i)#define for2(i,a,n) for(int i=(a);i<(n);++i)#define for3(i,a,n) for(int i=(a);i>=(n);--i)#define for4(i,a,n) for(int i=(a);i>(n);--i)#define CC(i,a) memset(i,a,sizeof(i))#define read(a) a=getint()#define print(a) printf("%d", a)#define dbg(x) cout << (#x) << " = " << (x) << endl#define error(x) (!(x)?puts("error"):0)#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }const double eps=1e-6;int dcmp(double x) { return abs(x)<eps?0:(x<0?-1:1); }struct ipt { double x, y; ipt(double _x=0, double _y=0) : x(_x), y(_y) {} };bool operator== (ipt &a, ipt &b) { return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0; }double icross(ipt &a, ipt &b, ipt &c) {static double x1, x2, y1, y2;x1=a.x-c.x; y1=a.y-c.y;x2=b.x-c.x; y2=b.y-c.y;return x1*y2-x2*y1;}double idot(ipt &a, ipt &b, ipt &c) {static double x1, x2, y1, y2;x1=a.x-c.x; y1=a.y-c.y;x2=b.x-c.x; y2=b.y-c.y;return x1*x2+y1*y2;}bool cmp(const ipt &a, const ipt &b) { return a.x==b.x?a.y<b.y:a.x<b.x; }void tu(ipt *p, ipt *s, int n, int &top) {sort(p, p+n, cmp);top=-1;rep(i, n) {while(top>0 && dcmp(icross(p[i], s[top], s[top-1]))>=0) --top;s[++top]=p[i];}static int k;k=top;for3(i, n-2, 0) {while(top>k && dcmp(icross(p[i], s[top], s[top-1]))>=0) --top;s[++top]=p[i];}if(n>1) --top;++top;}bool PonS(ipt &a, ipt &p1, ipt &p2) { if(a==p1 || a==p2) return true; return dcmp(icross(a, p1, p2))==0 && dcmp(idot(p1, p2, a))<0; }const int N=505, oo=~0u>>2;int d[N][N], n, m, ans=oo;ipt t[N], a[N], b[N];bool checktu() {rep(i, n) rep(j, m) if(dcmp(icross(b[j], a[i], a[i+1]))<0) return 0;return 1;}bool check(ipt &x, ipt &y) {rep(i, m) if(dcmp(icross(b[i], x, y))<0) return 0;return 1;}bool spj() {int flag;rep(i, n) { flag=1; rep(j, m) if(!(a[i]==b[j])) { flag=0; break; } if(flag) { puts("1"); return 1; } }rep(i, n) for1(j, i+1, n-1) {flag=1;rep(k, m) if(!PonS(b[k], a[i], a[j])) { flag=0; break; }if(flag) { puts("2"); return 1; }}return 0;}int main() {read(n); read(m); double tt;rep(i, n) scanf("%lf%lf%lf", &t[i].x, &t[i].y, &tt);rep(i, m) scanf("%lf%lf%lf", &b[i].x, &b[i].y, &tt);tu(t, a, n, n); a[n]=a[0];if(!checktu()) { puts("-1"); return 0; }if(spj()) return 0;rep(i, n) rep(j, n) d[i][j]=oo;rep(i, n) rep(j, n) if(i!=j) { if(check(a[i], a[j])) d[i][j]=1; else if(j>i) break; }rep(k, n) rep(i, n) rep(j, n) d[i][j]=min(d[i][j], d[i][k]+d[k][j]);rep(i, n) ans=min(ans, d[i][i]);printf("%d\n", ans<=2?-1:ans);return 0;}

  

 

 

 

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 Input3 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.1Sample Output2HINT Source

 

 

【BZOJ】1027: [JSOI2007]合金(凸包+floyd)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.