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 ...
随机推荐
- 在serviceImpl里使用自身的方法
@Service("tbLeaveRegisterService")@Transactionalpublic class TbLeaveRegisterServiceImpl ex ...
- android提供ToolBar实现划动菜单的陷阱
代码如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android: ...
- CentOS 7.0下面安装并配置Spark
安装环境: 虚拟机:VMware® Workstation 8.0.1(网络桥接) OS:CentOS 7 JDK版本:jdk-7u79-linux-x64.tar Scala版本:scala-2.1 ...
- android动态调试samli代码(转)
转载自看雪http://bbs.pediy.com/showthread.php?t=189610,非常感谢原作者分享! 跟踪apk一般的做法是在反编译的smali代码中插入log输出,然后重新编译运 ...
- MAC下配置ZSH
Mac的Terminal出了bash还配备了zsh模式,相比于bash,zsh的界面更加简单精致,用户名直接省略,用一个小箭头代替,而且箭头的颜色还可以指示命令的对错:路径和文件名的自动补全功能也十分 ...
- [LeetCode]题解(python):052-N-Queens II
题目来源 https://leetcode.com/problems/n-queens-ii/ Follow up for N-Queens problem. Now, instead outputt ...
- 被druid折磨的够呛
使用德鲁伊数据库连接池 数据源是这么配的 <!--数据源--> <bean id="dataSource" class="com.alibaba.dru ...
- LeetCode Longest Increasing Path in a Matrix
原题链接在这里:https://leetcode.com/problems/longest-increasing-path-in-a-matrix/ Given an integer matrix, ...
- svn使用dump和hotcopy进行脚本备份
[root@svn shell]# cat repolist dev-arcdev-bmdev-crmdev-paydev-pmdev-portaldev-riskhrproductqarep-op ...
- 在Visual Studio 2015中运行OPENGL
Starting an OpenGL project in VS 2015 is really easy, thanks to the NupenGL.Core nuget package. Here ...