uva 10498 Happiness(线性规划)

题意:n种食物m个人,已知每种食物的单价,每个人吃每种食物的愉快值,每个人的愉快值上限,求花钱买食物所花钱的最大值;

思路:线性规划;可得标准形式,带入模版;

标准形式即由不等式构成的方程组,松弛形式即由等式构成的方程组;

等式转不等式,用既大于等于又小于等于表示;不等式转等式,用增加一个变量,新增变量大于0来表示;

#include <iostream>
#include <cstdio>
#include <math.h>
using namespace std;

const double dinf=1e10;
const int MAX=55;

int n,m,B[MAX],N[MAX];
double A[MAX][MAX],b[MAX],c[MAX],v;
double ans[MAX];

int sgn(double x)
{
    if(x>1e-8) return 1;
    if(x<-1e-8) return -1;
    return 0;
}

void init()
{
    int i,j;
    for(i=1;i<=n;i++) N[i]=i;
    for(i=1;i<=m;i++) B[i]=n+i;
    v=0;
}

void pivot(int l,int e)  //主元,输入松弛型线性规划
{
    int i,j;
    double temp=A[l][e];
    b[l]/=temp; A[l][e]=1/temp;
    for(i=1;i<=n;i++) if(i!=e) A[l][i]/=temp;
    for(i=1;i<=m;i++) if(i!=l)
    {
        b[i]-=A[i][e]*b[l];
        for(j=1;j<=n;j++) if(j!=e) A[i][j]-=A[i][e]*A[l][j];
        A[i][e]=-A[i][e]/temp;
    }
    v+=b[l]*c[e];
    for(i=1;i<=n;i++) if(i!=e) c[i]-=c[e]*A[l][i];
    c[e]*=-A[l][e];
    swap(B[l],N[e]);
}

void simplex()  //单纯型法,输入标准型线性规划
{
    int i,j,k,x;
    int l,s;
    double temp,temp1,temp2,temp3;

    while(1)
    {
        temp2=-dinf; s=-1;
        for(i=1;i<=n;i++) if(sgn(c[i])>0)
        {
            temp=dinf;
            for(k=1;k<=m;k++) if(sgn(A[k][i])>0)
            {
                temp3=b[k]/A[k][i];
                if(temp3<temp) temp=temp3,x=k;
            }
            if(temp2<temp*c[i])
            {
                s=i,l=x,temp2=temp*c[i];
            }
        }
        if(s==-1) break;
        pivot(l,s);
    }
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++) if(B[j]==i) break;
        if(j<=m) ans[i]=b[j];
        else ans[i]=0;
    }
}

int main()
{
    while(scanf("%d%d",&n,&m)!=-1)
    {
        int i,j;
        for(i=1;i<=n;i++) scanf("%lf",&c[i]);
        for(i=1;i<=m;i++)
        {
            for(j=1;j<=n;j++) scanf("%lf",&A[i][j]);
            scanf("%lf",&b[i]);
        }
        init();
        simplex();
        printf("Nasa can spend %.0lf taka.\n",ceil(v*m));
    }
    return 0;
}
时间: 2024-08-25 10:52:09

uva 10498 Happiness(线性规划)的相关文章

uva 11971 - Polygon(线性规划)

题目连接:uva 11971 - Polygon 题目大意:给定一个长度为N的线段,要求切K刀,分成K+1个线段,问能组成K+1边形的概率. 解题思路:K条线段能组成K边形的条件为任意一条边小于其他所有边的和,因为是求概率,所以和N无关. 根据高中线性规划的知识,以二维为例: 所以有ans=2K?K?12K #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typ

数学(线性规划):UVAoj 10498 Happiness

Problem GHappiness! Input: standard inputOutput: standard outputTime Limit: 3 seconds Prof. Kaykobad has given Nasa the duty of buying some food for the ACM contestents. Nasa decided to buy n different items. He then asked each of the m contestents h

[UVA] 10167 - Birthday Cake

 Problem G. Birthday Cake  Background Lucy and Lily are twins. Today is their birthday. Mother buys a birthday cake for them.Now we put the cake onto a Descartes coordinate. Its center is at (0,0), and the cake's length of radius is 100. There are 2N

UVA 12325 Zombie&#39;sTreasureChest

看上去非常像背包的问题,但是体积太大了. 线性规划的知识,枚举附近点就行了,优先选性价比高的, 宝物有两种体积为S0,价值V0,体积S1,价值V1. 枚举分以下几种: 1:枚举拿宝物1的数量,然后尽量多拿宝物2:O(N/S0) 2:枚举拿宝物2的数量,同上:O(N/S1) 3.贪心,尽量选性价比高的 令gcd(S0,S1)= t,S1/t*S0 = S0/t*S1:体积相同的情况下尽量选价值高的,如果S1*V0>S0*V1大,那么枚举拿宝物2的数量,最多S0/t-1个否则一定可以换成S1/t个宝

UVA 10085(bfs+康拓展开)八数码问题

Description Problem A The Most Distant State Input: standard input Output: standard output The 8-puzzle is a square tray in which eight square tiles are placed. The remaining ninth square is uncovered. Each tile has a number on it. A tile that is adj

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f

[UVa] Palindromes(401)

UVA - 401 Palindromes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDED