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 <script type="math/tex" id="MathJax-Element-10674">t</script>的离原点近的那个交点即可。

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值