UVA 10498 Happiness(线性规划-单纯形)
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(线性规划-单纯形)的更多相关文章
- UVa 10498 Happiness! (线性规划)
题意 将N种食品分给m个参赛选手,一个单位的某食品给某个选手一定满足度,每个选手有一个最大满足度.为了避免浪费,分给每一个选手的食品都不超越选手的满足度.已知的各种食品的单价,求最多可以花的钱. 思路 ...
- 数学(线性规划):UVAoj 10498 Happiness
Problem GHappiness! Input: standard inputOutput: standard outputTime Limit: 3 seconds Prof. Kaykobad ...
- 【UOJ #179】线性规划 单纯形模板
http://uoj.ac/problem/179 终于写出来了单纯性算法的板子,抄的网上大爷的qwq 辅助线性规划找非基变量时要加个随机化才能A,我也不知道为什么,卡精度吗? 2017-3-6UPD ...
- 【Uva 10498】满意值
Description Kaykobad教授把为ACM选手买饭的任务交给了Nasa.Nasa决定买n种不同的食物.然后他询问了m名选手对每种食物的需求量.选手们当然不会给出任何符合逻辑的回答,他们只是 ...
- 【UOJ#179】线性规划 单纯形
题目链接: http://uoj.ac/problem/179 Solution 就是单纯形模板题,这篇博客就是存一下板子. Code #include<iostream> #includ ...
- UOJ.179.线性规划(单纯形)
题目链接 这写得还不错:http://www.cnblogs.com/zzqsblog/p/5457091.html 引入基变量\(x_{i+n}\),将约束\(\sum_{i=1}^m a_{ij} ...
- UVA10498 Happiness 【单纯形】
题目链接 UVA10498 题解 模板题 #include<algorithm> #include<iostream> #include<cstdlib> #inc ...
- 线性规划VB求解
线性规划VB求解 Rem 定义动态数组 Dim a() As Single, c() As Single, b() As Single, cb() As Single Dim aa() As Sing ...
- 机器学习-线性规划(LP)
线性规划问题 首先引入如下的问题: 假设食物的各种营养成分.价格如下表: Food Energy(能量) Protein(蛋白质) Calcium(钙) Price Oatmeal(燕麦) 110 4 ...
随机推荐
- javascript值和引用
JavaScript引用指向的是值. 简单值(即标量基本类型值,基本类型值,js中6类,null.undefined.boolean.number.string和symbol)总是通过值复制的方式来赋 ...
- Ajax如何实现跨域问题
一个域名的组成 http:// www . abc.com : 8080 /scripts/jquery.js 协议 子域名 主域名 端口号 请求资源地址 当协议.子域名.主域名.端口号中任意一个不同 ...
- Linux 性能工具 - sar学习
简介 sar是一款在linux下的性能工具,可以观察到CPU,内存,IO,运行队列,每秒上下文切换等信息. 软件工具安装 #Ubuntu sudo apt-get install sysstat # ...
- Linux上使用SMART检测硬盘
SMART(Self-Monitoring, Analysis, and Reporting Technology)是一种普及度比较高的磁盘分析检测工具,磁盘运行过程中,该工具搜集磁盘的状态参数,如型 ...
- LeetCode Majority Element I && II
原题链接在这里:Majority Element I,Majority Element II 对于Majority Element I 来说,有多重解法. Method 1:最容易想到的就是用Hash ...
- 实践JAVA wait(), notify(),sleep方法--一道多线程的面试题
建立三个线程,A线程打印10次A,B线程打印10次B,C线程打印10次C,要求线程同时运行,交替打印10次ABC. 这个问题用Object的wait(),notify()就可以很方便的解决. publ ...
- saltstack之(三)认证管理
salt-master和salt-minion之间需要进行认证,认证之后salt-master才能管理salt-minion. 1.在node1:[root@node1 ~]# egrep -v '^ ...
- MySQL学习笔记——复制的实现原理
1.三个线程 MYSQL复制是从主服务器复制到一个或多个从服务器的异步过程,在主服务器与从服务器之间实现整个复制过程主要由三个线程来实现,其中一个线程I\O在主服务器器端,另两个线程(SQL线程和I\ ...
- 话说 依赖注入(DI) or 控制反转(IoC)
科普:首先依赖注入和控制反转说的是同一个东西,是一种设计模式,这种设计模式用来减少程序间的耦合,鄙人学习了一下,看TP官网还没有相关的文章,就写下这篇拙作介绍一下这种设计模式,希望能为TP社区贡献一些 ...
- PHP 数组和字符串互相转换实现方法
$array=explode(separator,$string); $string=implode(glue,$array);