码迷,mamicode.com
首页 > 其他好文 > 详细

sgu278:Fuel(线性规划)

时间:2015-06-22 15:03:05      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

题目大意:
      对于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;
}

sgu278:Fuel(线性规划)

标签:

原文地址:http://blog.csdn.net/qq_20118433/article/details/46592873

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!