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

Problem G
Happiness!

Input: standard
input
Output: standard output
Time 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 how much of each item they want
to eat. They could not give any logical answer, they only want as much as they
wish! Nasa knows quite well that they would only waste their food if they get as
much as they want. He was determined not to let that happen.

So he tactfully found out from each of the
contestents how much ‘happiness‘ one gets from each piece of each item and what
is the ‘total happiness‘ over which one wastes food. It may be the case that
someone gets ‘zero‘ ‘happiness‘ on some item(s). He decided that he would never
let anyone have such amount of food that exceeds his ‘total happiness‘. He
planned that he would give someone even a fraction of a piece of item, but never
give anyone more than he needed!

He also decided that each would get exactly the
same amount of each item so that no one can complain against him.

After planning all these, he finally realized that
he has an infinite amount of money and hence, he would spend as much money as he
can.

Input

Input contains data collected by Nasa on
several days.

For each day,

The first line contains the
integers n(3<=n<=20) and m(3<=m<=20).

The next line contains n
real numbers, the per unit price of each item.

Each of the next m lines
contain data (n+1 real numbers) of each contestents: first n are ‘happiness‘ got
from each item and the last one is the ‘total happiness‘.

Output

For the data
collected in each day print in a single line the maximum amount of money Nasa
can spend in taka rounded up to nearest integer. You can assume that there will be no such input which may cause
serious floating point errors.

Sample Input

3 3
1 0.67 1.67
1 2 1 430
3 0 2 460
1 4 0 420

Sample Output

Nasa can spend 1354 taka.    这是线性规划模版题。
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <cmath>
 5 const double EPS = 1e-10;
 6 const int MAXN = 55;
 7 const int INF = 0x3fff3fff;
 8
 9 inline int sgn(double x) {
10     return (x>EPS)-(x<-EPS);
11 }
12
13 double A[MAXN][MAXN];
14 double b[MAXN],c[MAXN];
15 int N[MAXN],B[MAXN];
16 int n,m;
17 double v;
18
19 bool init() {
20     N[0]=B[0]=v=0;
21     for(int i=1;i<=n;++i)N[++N[0]]=i;
22     for(int i=1;i<=m;++i)B[++B[0]]=n+i;
23     return true;
24 }
25
26 void pivot(int l,int e){
27     b[e]=b[l]/A[l][e];
28     A[e][l]=1.0/A[l][e];
29     for(int i=1;i<=N[0];++i){
30         int &x=N[i];
31         if(x!=e)A[e][x]=A[l][x]/A[l][e];
32     }
33     for(int i=1;i<=B[0];++i)if(B[i]!=){
34         int y=B[i];
35         b[y]-=A[y][e]*b[e];
36         A[y][l]=-A[y][e]*A[e][l];
37         for(int j=1;j<=N[0];++j){
38             int x=N[j];
39             if(x!=e)A[y][x]-=A[e][x]*A[y][e];
40         }
41     }
42     v+=b[e]*c[e];
43     c[l]=-A[e][l]*c[e];
44     for(int i=1;i<=N[0];++i) {
45         int x=N[i];
46         if(x!=e)c[x]-=A[e][x]*c[e];
47     }
48     for(int i=1;i<=N[0];++i)if(N[i]==e)N[i]=l;
49     for(int i=1;i<=B[0];++i)if(B[i]==l)B[i]=e;
50 }
51
52 bool simplex() {
53     while(true) {
54         int e=MAXN;
55         for(int i=1;i<=N[0];++i) {
56             int x=N[i];
57             if(sgn(c[x])>0&&x<e)e=x;
58         }
59         if(e==MAXN) break;
60         double delta=-1;
61         int l=MAXN;
62         for(int i=1;i<=B[0];++i) {
63             int y=B[i];
64             if(sgn(A[y][e])>0){
65                 double tmp=b[y]/A[y][e];
66                 if(delta==-1||sgn(tmp-delta)<0||(sgn(tmp-delta)==0&&y<l)){
67                     delta=tmp;
68                     l=y;
69                 }
70             }
71         }
72         if(l==MAXN) return false;
73         pivot(l,e);
74     }
75     return true;
76 }
77
78 int main() {
79     while(scanf("%d%d",&n,&m)!=EOF) {
80         for(int i=1;i<=n;++i)
81             scanf("%lf",&c[i]);
82         for(int i=1;i<=m;++i){
83             for(int j=1;j<=n;++j)
84                 scanf("%lf",&A[n+i][j]);
85             scanf("%lf",&b[n+i]);
86         }
87         init();
88         simplex();
89         printf("Nasa can spend %d taka.\n",(int)ceil(v*m));
90     }
91 }
时间: 2024-10-13 04:47:22

数学(线性规划):UVAoj 10498 Happiness的相关文章

uva 10498 Happiness(线性规划)

题意:n种食物m个人,已知每种食物的单价,每个人吃每种食物的愉快值,每个人的愉快值上限,求花钱买食物所花钱的最大值: 思路:线性规划:可得标准形式,带入模版: 标准形式即由不等式构成的方程组,松弛形式即由等式构成的方程组: 等式转不等式,用既大于等于又小于等于表示:不等式转等式,用增加一个变量,新增变量大于0来表示: #include <iostream> #include <cstdio> #include <math.h> using namespace std;

高等工程数学 线性规划部分 作业

十线性规划模型与理论简介 某公司面临一个是外包协作还是自行生产的问题.该公司生产甲.乙.丙三种产品,都要经过铸造.机加工和装配三个车间.甲.乙两种产品的铸件可以外包协作,亦可以自行生产,但产品丙必须本厂铸造才能保证质量.数据如下表.问:公司为了获得最大利润,甲.乙.丙三种产品各生产多少件?甲.乙两种产品的铸造中,由本公司铸造和由外包协作各应多少件? 甲 乙 丙 资源限制 铸造工时(小时/件) 5 10 7 8000 机加工工时(小时/件) 6 4 8 12000 装配工时(小时/件) 3 2 2

二分,倍增的一些思考(lost my music:可持久化栈)

浅谈二分 来自8,17考试模拟24. 本题: 单调凸包.(找凸包方向:联系高考数学线性规划) 弹栈操作是一个个向后弹的.序列转换为树上.对于树上结构,只需记录父子关系,即可还原出一整棵树.因为要可持久化,那么这里的栈也变为了树状.只需记录在栈里的父亲即可.甚至不用开一个数组作为栈.由于单调性,搭配倍增使用,效果更佳. 二分,倍增的一些思考: 浅谈二分,倍增1.那么普通的数组类型栈由于单调性,且是序列上的连续,可以用二分和倍增快速pop    (这里的二分指l,r,while循环,check(mi

数学建模培训第一天---线性规划

线性规划:在一定线性约束条件(s.t. --- subject to)下,求解目标函数的极值 以下截图都是 司守奎 的<数学建模算法与程序>中的内容 线性规划的公式为 [x, fval] = (c, A, b, aeq, beq, lb, ub) (标准形式为求解最小值, 所以如果要求最大值的话将c改成-c就好了,然后把得到的答案取个负就得到了最大值: 标准的A b满足的条件为Ax<=b, 所以如果题目给的是大于的话,取个负,然后改变下符号得≤就可以用公式了) 注意:有些问题列出的公式可

【数学建模】线性规划各种问题的Python调包方法

关键词:Python.调包.线性规划.指派问题.运输问题.pulp.混合整数线性规划(MILP) 注:此文章是线性规划的调包实现,具体步骤原理请搜索具体解法.   本文章的各个问题可能会采用多种调用方法,为什么?因为这些包各有特点,有些语法特别像matlab,只要稍稍改变即可达成代码交换:而有些包利用了python本身的特性,在灵活度与代码的可读性上更高.我认为这些包各有优劣,各位各持所需吧.   看了本文章能做到什么?你可以在本文章内学到线性规划的几个问题的求解方式,并学会如何用pulp包解决

数学(线性规划): ZJOI2013 防守战线

偷懒用的线性规划. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 const int maxr=1010; 6 const int maxc=10010; 7 8 int n,m,nxt[maxc]; 9 int a[maxr][maxc]; 10 11 12 void Pivot(int l,int e){ 13 int pre=maxc-

使用Python scipy linprog 线性规划求最大值或最小值(使用Python学习数学建模笔记)

函数格式 scipy.optimize.linprog(c, A_ub=None, b_ub=None, A_eq=None, b_eq=None, bounds=None, method='simplex', callback=None, options=None) 今天阅读数据建模第一章线性规划问题,问题描述如下: 通过介绍我们知道了线性规划,就是目标函数及约束条件均为线性函数. 通过画图我们可知,X1,X2的最优解为2,6,目标值为26. 我们如何时候这个scipy的公式来计算这个值呢:

数学建模----线性规划

我理解的线性规划就是在给定的一些线性方程可以列出的约束条件下求解目标函数的极值. 它在matlab中的标准型为: [x,fval]=linprog(c,A,b,Aeq,beq,LB,UB,X0,OPTIONS)  ,fval为返回的目标函数的值 默认求最小值,求最大值的话,把c换成-c就好了(最大值取个﹣就是最小值了)

数学建模算法(一):线性规划

1.(DVD 式子 代码 MODEL: SETS: CN/C1..C1000/:B; DN/D1..D100/:DVNUM; LINKS(CN,DN):SATI,X; ENDSETS DATA: SATI,DVNUM[email protected]('D:\LINGO\DATA\b2005table2.xlsx'); @OLE('D:\LINGO\DATA\b2005table2.xlsx','X','B')= X,B; @TEXT()=@STATUS(); ENDDATA SA=@SUM(L