sgu278:Fuel(線性規劃)

題目大意:
       對於n 個元素,每個元素有三個非負整數屬性a,b,c ,總共有兩個正整數限制A,B ,求一個非負實數解集X ,使得ΣaixiA,ΣbixiBΣcixi 最大。

分析:
       我們很明顯可以把三變量a,b,c 化成兩變量d=ac,e=bc( 也就是取一個共同的單位1) ,以及y 表示cx
      ΣaixiAΣdiyiAΣbixiBΣeiyiB
       因爲x 是實數,所以我們的最優解要麼只取一個元素,要麼取的元素滿足Σdiyi=A,Σeiyi=B
       將每個元素考慮成平面上一個點(d,e) ,表示一個單位該元素消耗爲(d,e) 。對於平面上任選的點集,(Σdizi,Σeizi),Σzi=1 就是這些點集構成圖包內的所有點。如果要滿足Σdiyi=A,Σeiyi=B ,這個點集構成的凸包與線段t=(A,B) 的交集就是組合成一個單位消耗(a,b),ab=AB(aA,bB) 的新元素的所有解g=(a,b) ,定義l(p) 爲線段p 的長度,那麼l(t)/l(g) 即爲這個解對應的答案。
       因爲l(t) 已經確定,我們要讓l(g) 儘量小,那麼只能是線段與凸包的交點處,因此我們求出整個凸包與線段t 的離原點近的那個交點即可。

AC code:

#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <string>
#include <sstream>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#define pb push_back
#define mp make_pair
#define sqr(x) ((x)*(x))
typedef long long LL;
typedef double DB;
typedef long double LD;
using namespace std;

const int MAXN = 75009;
const DB eps = 1e-9;

int n, A, B;
struct pot
{
    DB x, y;
    pot() {x = y = 0;}
    pot(DB _x, DB _y):x(_x),y(_y){}

    friend pot operator + (const pot &a, const pot &b) {return pot(a.x+b.x, a.y+b.y);}
    friend pot operator - (const pot &a, const pot &b) {return pot(a.x-b.x, a.y-b.y);}
    friend pot operator * (const pot &a, DB k) {return pot(a.x*k, a.y*k);}
    friend pot operator / (const pot &a, DB k) {return pot(a.x/k, a.y/k);}
    friend DB operator * (const pot &a, const pot &b) {return a.x*b.x+a.y*b.y;}
    friend DB operator ^ (const pot &a, const pot &b) {return a.x*b.y-a.y*b.x;}
    friend bool operator == (const pot &a, const pot &b) {return fabs(a.x-b.x) <= eps && fabs(a.y-b.y) <= eps;}
    friend bool operator < (const pot &a, const pot &b)
    {
        if(fabs(a.x-b.x) <= eps) return a.y < b.y;
        else return a.x < b.x;
    }
    DB size() {return sqrt(sqr(x)+sqr(y));}

}node[MAXN], o, t;

pot st[MAXN];
int top;

DB ans;

bool cross(const pot &a, const pot &b, const pot &c, const pot &d)
{
    return ((c-a)^(b-a))*((d-a)^(b-a)) <= 0 && ((b-c)^(d-c))*((a-c)^(d-c)) <= 0;
}

pot get(const pot &a, const pot &b, const pot &c, const pot &d)
{
    pot p = b-a, q = d-c, w = a-c;
    DB k = (w^q)/(q^p);
    return a+p*k;
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    scanf("%d%d%d", &n, &A, &B);
    o = pot(0, 0), t = pot(A, B);
    for(int i = 1; i <= n; ++i)
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        node[i] = pot((DB)a/c, (DB)b/c);
        ans = max(ans, min(A/node[i].x, B/node[i].y));
    }
    sort(node+1, node+n+1);
    st[++top] = node[1], st[++top] = node[2];
    for(int i = 3; i <= n; ++i)
    {
        if(node[i] == node[i-1]) continue;
        while(top >= 2 && ((st[top]-node[i])^(st[top-1]-node[i])) < 0) top--;
        st[++top] = node[i];
    }
    int j = top;
    for(int i = n-1; i >= 1; --i)
    {
        if(node[i] == node[i-1]) continue;
        while(top-1 >= j && ((st[top]-node[i])^(st[top-1]-node[i])) < 0) top--;
        st[++top] = node[i];
    }
    st[0] = st[top];

    for(int i = 0; i < top; ++i)
        if(cross(st[i], st[i+1], o, t))
            ans = max(ans, t.size()/get(st[i], st[i+1], o, t).size());

    printf("%.6lf\n", ans);

    #ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章