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 ...
随机推荐
- bootstrap响应式实用工具
- 低功耗蓝牙4.0BLE编程-nrf51822开发(9)
Android 4.3以后的系统自动支持蓝牙4.0规范的低功耗蓝牙(BLE).在android4.3之前,蓝牙4.0支持是由手机厂家加入支持的,接口各异,导致开发一个支持蓝牙4.0程序支持市面上的手机 ...
- 常见的Mule Esb下载地址
http://www.myexception.cn/open-source/1832157.html
- Visual Studio 2010扩展让JS与CSS实现折叠
在Visaul Studio 2010中写js或css代码,缺少像写C#代码时的那种折叠功能,当代码比较多时,就很不方便. 今天发现,已经有VS2010扩展支持这个功能,它就是——JSEnhancem ...
- Java高级之线程同步
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 关于实现多线程的意义,"从业四年看并发"一文已经讲述,而本篇主要讲一下常用的设计 ...
- JDK中的URLConnection参数详解
针对JDK中的URLConnection连接Servlet的问题,网上有虽然有所涉及,但是只是说明了某一个或几个问题,是以FAQ的方式来解决的,而且比较零散,现在对这个类的使用就本人在项目中的使用经验 ...
- [LeetCode]题解(python):038-Count and Say
题目来源 https://leetcode.com/problems/count-and-say/ The count-and-say sequence is the sequence of inte ...
- link标签和script标签跑到body下面,网页顶部有空白
用UltraEdit的16进制编辑模式查看代码,都是EF BB BF开头的,说明都是带BOM的.我手动的将所有文件转成UTF-8 without BOM.页面终于正常了.link,script标签乖乖 ...
- sublime text2 操作及插件
sublime text2 1. 文件快速导航: 这是sublime上面很好用的功能之一,ctrl+p可以调出窗口,菜单上的解释是gotoanythings ,确实如其所言,调出窗口后,直接输入关键字 ...
- LeetCode Shortest Word Distance II
原题链接在这里:https://leetcode.com/problems/shortest-word-distance-ii/ 题目: This is a follow up of Shortest ...