Description

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 mcontestents 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.

题目大意:某人要买套餐给m个人吃,每个人吃的套餐必须都是一样的。套餐由n种食品组成,每种食品的单位价格是已知的。由于口味不同,每个人每得到一单位的某种食品获得的满足度可能不一样,这些也是已知的。每个人满足度是有上限的,上限已知。求在不超过任何人的满足度上限的条件下,此人最多能花多少钱。(某种食品可以买实数单位)

思路:线性规划的模板题。可以参考IOI国家集训队论文《浅谈信息学竞赛中的线性规划——简洁高效的单纯形法实现与应用》(上面的题目大意也是来自这里……)或者《算法导论》。

代码(32MS):

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std; const double EPS = 1e-;
const int MAXN = ;
const int INF = 0x3fff3fff; inline int sgn(double x) {
return (x > EPS) - (x < -EPS);
} double A[MAXN][MAXN], tA[MAXN][MAXN];
double b[MAXN], tb[MAXN], c[MAXN], tc[MAXN];
int N[MAXN], B[MAXN];
int n, m;
double v; bool init() {
N[] = B[] = v = ;
for(int i = ; i <= n; ++i) N[++N[]] = i;
for(int i = ; i <= m; ++i) B[++B[]] = n + i;
return true;
} void pivot(int l, int e) {
tb[e] = b[l] / A[l][e];
tA[e][l] = 1.0 / A[l][e];
for(int i = ; i <= N[]; ++i)
if(N[i] != e) tA[e][N[i]] = A[l][N[i]] / A[l][e];
for(int i = ; i <= B[]; ++i) {
tb[B[i]] = b[B[i]] - A[B[i]][e] * tb[e];
tA[B[i]][l] = -A[B[i]][e] * tA[e][l];
for(int j = ; j <= N[]; ++j)
if(N[j] != e) tA[B[i]][N[j]] = A[B[i]][N[j]] - tA[e][N[j]] * A[B[i]][e];
}
v += tb[e] * c[e];
tc[l] = -tA[e][l] * c[e];
for(int i = ; i <= N[]; ++i)
if(N[i] != e) tc[N[i]] = c[N[i]] - tA[e][N[i]] * c[e];
for(int i = ; i <= N[]; ++i) if(N[i] == e) N[i] = l;
for(int i = ; i <= B[]; ++i) if(B[i] == l) B[i] = e;
for(int i = ; i <= B[]; ++i) {
for(int j = ; j <= N[]; ++j)
A[B[i]][N[j]] = tA[B[i]][N[j]];
b[B[i]] = tb[B[i]];
}
for(int i = ; i <= N[]; ++i) c[N[i]] = tc[N[i]];
} bool simplex() {
while(true) {
int e = MAXN;
for(int i = ; i <= N[]; ++i)
if(c[N[i]] > EPS && N[i] < e) e = N[i];
if(e == MAXN) break;
double delta = -;
int l = MAXN;
for(int i = ; i <= B[]; ++i) {
if(sgn(A[B[i]][e]) > ) {
double tmp = b[B[i]] / A[B[i]][e];
if(delta == - || sgn(tmp - delta) < || (sgn(tmp - delta) == && B[i] < l)) {
delta = tmp;
l = B[i];
}
}
}
if(l == MAXN) return false;
pivot(l, e);
}
return true;
} int main() {
while(scanf("%d%d", &n, &m) != EOF) {
for(int i = ; i <= n; ++i) scanf("%lf", &c[i]);
for(int i = ; i <= m; ++i) {
for(int j = ; j <= n; ++j) scanf("%lf", &A[n + i][j]);
scanf("%lf", &b[n + i]);
}
init();
simplex();
printf("Nasa can spend %d taka.\n", (int)ceil(v * m));
}
}

研究发现那个用来保存新的矩阵的数组并不是必要的,于是删掉。

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std; const double EPS = 1e-;
const int MAXN = ;
const int INF = 0x3fff3fff; inline int sgn(double x) {
return (x > EPS) - (x < -EPS);
} double A[MAXN][MAXN];
double b[MAXN], c[MAXN];
int N[MAXN], B[MAXN];
int n, m;
double v; bool init() {
N[] = B[] = v = ;
for(int i = ; i <= n; ++i) N[++N[]] = i;
for(int i = ; i <= m; ++i) B[++B[]] = n + i;
return true;
} void pivot(int l, int e) {
b[e] = b[l] / A[l][e];
A[e][l] = 1.0 / A[l][e];
for(int i = ; i <= N[]; ++i) {
int &x = N[i];
if(x != e) A[e][x] = A[l][x] / A[l][e];
}
for(int i = ; i <= B[]; ++i) {
int &y = B[i];
b[y] -= A[y][e] * b[e];
A[y][l] = -A[y][e] * A[e][l];
for(int j = ; j <= N[]; ++j) {
int &x = N[j];
if(x != e) A[y][x] -= A[e][x] * A[y][e];
}
}
v += b[e] * c[e];
c[l] = -A[e][l] * c[e];
for(int i = ; i <= N[]; ++i) {
int &x = N[i];
if(x != e) c[x] -= A[e][x] * c[e];
}
for(int i = ; i <= N[]; ++i) if(N[i] == e) N[i] = l;
for(int i = ; i <= B[]; ++i) if(B[i] == l) B[i] = e;
} bool simplex() {
while(true) {
int e = MAXN;
for(int i = ; i <= N[]; ++i) {
int &x = N[i];
if(sgn(c[x]) > && x < e) e = x;
}
if(e == MAXN) break;
double delta = -;
int l = MAXN;
for(int i = ; i <= B[]; ++i) {
int &y = B[i];
if(sgn(A[y][e]) > ) {
double tmp = b[y] / A[y][e];
if(delta == - || sgn(tmp - delta) < || (sgn(tmp - delta) == && y < l)) {
delta = tmp;
l = y;
}
}
}
if(l == MAXN) return false;
pivot(l, e);
}
return true;
} int main() {
while(scanf("%d%d", &n, &m) != EOF) {
for(int i = ; i <= n; ++i) scanf("%lf", &c[i]);
for(int i = ; i <= m; ++i) {
for(int j = ; j <= n; ++j) scanf("%lf", &A[n + i][j]);
scanf("%lf", &b[n + i]);
}
init();
simplex();
printf("Nasa can spend %d taka.\n", (int)ceil(v * m));
}
}

UVA 10498 Happiness(线性规划-单纯形)的更多相关文章

  1. UVa 10498 Happiness! (线性规划)

    题意 将N种食品分给m个参赛选手,一个单位的某食品给某个选手一定满足度,每个选手有一个最大满足度.为了避免浪费,分给每一个选手的食品都不超越选手的满足度.已知的各种食品的单价,求最多可以花的钱. 思路 ...

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

    Problem GHappiness! Input: standard inputOutput: standard outputTime Limit: 3 seconds Prof. Kaykobad ...

  3. 【UOJ #179】线性规划 单纯形模板

    http://uoj.ac/problem/179 终于写出来了单纯性算法的板子,抄的网上大爷的qwq 辅助线性规划找非基变量时要加个随机化才能A,我也不知道为什么,卡精度吗? 2017-3-6UPD ...

  4. 【Uva 10498】满意值

    Description Kaykobad教授把为ACM选手买饭的任务交给了Nasa.Nasa决定买n种不同的食物.然后他询问了m名选手对每种食物的需求量.选手们当然不会给出任何符合逻辑的回答,他们只是 ...

  5. 【UOJ#179】线性规划 单纯形

    题目链接: http://uoj.ac/problem/179 Solution 就是单纯形模板题,这篇博客就是存一下板子. Code #include<iostream> #includ ...

  6. UOJ.179.线性规划(单纯形)

    题目链接 这写得还不错:http://www.cnblogs.com/zzqsblog/p/5457091.html 引入基变量\(x_{i+n}\),将约束\(\sum_{i=1}^m a_{ij} ...

  7. UVA10498 Happiness 【单纯形】

    题目链接 UVA10498 题解 模板题 #include<algorithm> #include<iostream> #include<cstdlib> #inc ...

  8. 线性规划VB求解

    线性规划VB求解 Rem 定义动态数组 Dim a() As Single, c() As Single, b() As Single, cb() As Single Dim aa() As Sing ...

  9. 机器学习-线性规划(LP)

    线性规划问题 首先引入如下的问题: 假设食物的各种营养成分.价格如下表: Food Energy(能量) Protein(蛋白质) Calcium(钙) Price Oatmeal(燕麦) 110 4 ...

随机推荐

  1. Eclipse下使用Ant 【转】

    官方在线帮助文档:http://ant.apache.org/manual/index.html 中文汉化 帮助文档:http://www.cnblogs.com/pengxl/archive/201 ...

  2. 低功耗蓝牙4.0BLE编程-nrf51822开发(9)

    Android 4.3以后的系统自动支持蓝牙4.0规范的低功耗蓝牙(BLE).在android4.3之前,蓝牙4.0支持是由手机厂家加入支持的,接口各异,导致开发一个支持蓝牙4.0程序支持市面上的手机 ...

  3. ssh2 php扩展

    如何通过PHP启动和关闭远程服务器上的某个软件,譬如Memcached.对于俺这个刚刚掌握PHP编程皮毛的菜鸟来说,最直接不过的想法就是用exec函数执行SSH命令呗,先把运行Apache+PHP的服 ...

  4. qt如何实现一个渐隐窗口呢(开启的时候他是从上往下渐渐显示)

    qt如何实现一个渐隐窗口呢?就是比如说开启的时候他是从上往下渐渐显示的,关闭的时候从下往上渐渐小时的http://stackoverflow.com/questions/19087822/how-to ...

  5. 【Java 基础篇】【第六课】接口interface

    Java提供的这个interface的语法,目的就是将接口从类中剥离出来,构成独立的主体. 首先加入我们定义了这个杯子接口: interface Cup { void addWater(int w); ...

  6. 简单的form表单

    效果 html <ul class="edit_list"> <li><em>*</em><span class=" ...

  7. 简单计算器--hdu1237(栈的运用)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1237  这是单纯的本题答案: #include<stdio.h> #define N 11 ...

  8. SQL Server 2008 R2【SET ANSI_PADDING填充属性】插入一条数据后,为何每一列都默认的在字符后多了几个空格

    当加入空格后查出 解决: 导致出现这样的现象的原因就是SET ANSI_PADDING选项. 这个选项只在数据表的字符串字段被更新或者新的数据行插入到表中的时候作用.它控制着SQL Server在遇到 ...

  9. Tomcat 处理请求时的中文乱码问题

    利用Tomcat8作为服务器,采用servlet接收前端请求后进行处理的过程中,前台请求中有中文时,中文信息变成了乱码. 经过调试和查阅,发现Tomcat在处理get请求和post请求是有区别的.参照 ...

  10. table tricks