您的位置:首页 > 产品设计 > UI/UE

sgu278:Fuel(线性规划)

2015-06-22 13:13 295 查看
题目大意:

~~~~~~对于nn个元素,每个元素有三个非负整数属性a,b,ca,b,c,总共有两个正整数限制A,BA,B,求一个非负实数解集XX,使得Σaixi≤A,Σbixi≤B\Sigma a_ix_i \leq A,\Sigma b_ix_i \leq B且Σcixi\Sigma c_ix_i最大。

分析:

~~~~~~我们很明显可以把三变量a,b,ca,b,c化成两变量d=ac,e=bcd=\frac{a}{c},e=\frac{b}{c}((也就是取一个共同的单位1)1),以及yy表示cxcx。

~~~~~~Σaixi≤A⇒Σdiyi≤A\Sigma a_ix_i \leq A \Rightarrow \Sigma d_iy_i \leq A,Σbixi≤B⇒Σeiyi≤B\Sigma b_ix_i \leq B \Rightarrow \Sigma e_iy_i \leq B。

~~~~~~因为xx是实数,所以我们的最优解要么只取一个元素,要么取的元素满足Σdiyi=A,Σeiyi=B\Sigma d_iy_i = A,\Sigma e_iy_i = B。

~~~~~~将每个元素考虑成平面上一个点(d,e)(d,e),表示一个单位该元素消耗为(d,e)(d,e)。对于平面上任选的点集,(Σdizi,Σeizi),Σzi=1(\Sigma d_iz_i,\Sigma e_iz_i),\Sigma z_i=1就是这些点集构成图包内的所有点。如果要满足Σdiyi=A,Σeiyi=B\Sigma d_iy_i = A,\Sigma e_iy_i = B,这个点集构成的凸包与线段t=(A,B)t=(A,B)的交集就是组合成一个单位消耗(a,b),ab=AB(a≤A,b≤B)(a,b),\frac{a}{b}=\frac{A}{B}(a\leq A,b\leq B)的新元素的所有解g=(a,b)g = (a,b),定义l(p)l(p)为线段pp的长度,那么l(t)/l(g)l(t)/l(g)即为这个解对应的答案。

~~~~~~因为l(t)l(t)已经确定,我们要让l(g)l(g)尽量小,那么只能是线段与凸包的交点处,因此我们求出整个凸包与线段tt的离原点近的那个交点即可。

AC code:

[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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: