数学(线性规划):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.
这是线性规划模版题。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
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)if(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));
}
}
数学(线性规划):UVAoj 10498 Happiness的更多相关文章
- UVA 10498 Happiness(线性规划-单纯形)
Description Prof. Kaykobad has given Nasa the duty of buying some food for the ACM contestents. Nasa ...
- UVa 10498 Happiness! (线性规划)
题意 将N种食品分给m个参赛选手,一个单位的某食品给某个选手一定满足度,每个选手有一个最大满足度.为了避免浪费,分给每一个选手的食品都不超越选手的满足度.已知的各种食品的单价,求最多可以花的钱. 思路 ...
- 数学:UVAoj 11174 Stand in a Line
Problem J Stand in a Line Input: Standard Input Output: Standard Output All the people in the bytela ...
- Android不规则点击区域详解
Android不规则点击区域详解 摘要 今天要和大家分享的是Android不规则点击区域,准确说是在视觉上不规则的图像点击响应区域分发. 其实这个问题比较简单,对于很多人来说根本不值得做为一篇博文写出 ...
- 【数学建模】线性规划各种问题的Python调包方法
关键词:Python.调包.线性规划.指派问题.运输问题.pulp.混合整数线性规划(MILP) 注:此文章是线性规划的调包实现,具体步骤原理请搜索具体解法. 本文章的各个问题可能会采用多种调用方 ...
- Python小白的数学建模课-03.线性规划
线性规划是很多数模培训讲的第一个算法,算法很简单,思想很深刻. 要通过线性规划问题,理解如何学习数学建模.如何选择编程算法. 『Python小白的数学建模课 @ Youcans』带你从数模小白成为国赛 ...
- 使用Python scipy linprog 线性规划求最大值或最小值(使用Python学习数学建模笔记)
函数格式 scipy.optimize.linprog(c, A_ub=None, b_ub=None, A_eq=None, b_eq=None, bounds=None, method='simp ...
- 数学(线性规划): ZJOI2013 防守战线
偷懒用的线性规划. #include <iostream> #include <cstring> #include <cstdio> using namespace ...
- Python数学建模系列(一):规划问题之线性规划
@ 目录 前言 线性规划 样例1:求解下列线性规划问题 scipy库求解 样例2:求解下列线性规划问题 pulp库求解 样例3.运输问题 说明 结语 前言 Hello!小伙伴! 非常感谢您阅读海轰的文 ...
随机推荐
- 关于SqlServer修改数据库常用信息的方法
--系统表里存放各个数据库属性信息的表之一SELECT name AS [Logical Name], physical_name AS [DB File Path],type_desc AS [Fi ...
- $HTTP_RAW_POST_DATA
这是手册里写的 总是产生变量包含有原始的 POST 数据.否则,此变量仅在碰到未识别 MIME 类型的数据时产生.不过,访问原始 POST 数据的更好方法是 php://input.$HTTP_RAW ...
- reactjs 入门
地址搜集:http://www.cocoachina.com/webapp/20150604/12035.html class 属性需要写成 className ,for 属性需要写成 htmlFor ...
- StudioStyle 使用 厌倦了默认的Visutal Studio样式了,到这里找一个酷的试试
厌倦了默认的Visutal Studio样式了,到这里找一个酷的试试 http://studiostyl.es/ 去下载个自己喜欢的编码样式吧 如果你有想法 有能力 可以自己去做一个自己喜欢的 OK ...
- base64加密解密文件
1 //字符串加密 -(void)demo1 { //普通的 8 bit二进制数据 NSString *str = @"hello world!"; //将字符串转换成二进制数据 ...
- C# div、css
目录: 1.Div+Css布局教程(-)CSS必备知识 注:本教程要求对html和css有基础了解. 一.CSS布局属性 Width:设置对象的宽度(width:45px). Height:设置对象的 ...
- SGU 176.Flow construction (有上下界的最大流)
时间限制:0.5s 空间限制:4M 题意: 有一个由管道组成的网络,有n个节点(n不大于100),1号节点可以制造原料,最后汇集到n号节点.原料通过管道运输.其中有一些节点有管道连接,这些管道都有着最 ...
- 织梦 dedecms 中LOOP 万能标签循环 调用 arcurl标签(获取链接)
在DEDECMS中,提供了loop万能循环标签,但是此循环标签只能循环出该表中的字段,而“[field:arcurl/]”链接标签并不能被解析出来,而DEDECMS官方论坛上也没有找到相关的解决办法, ...
- operation 多线程
2.Cocoa Operation 优点:不需要关心线程管理,数据同步的事情.Cocoa Operation 相关的类是 NSOperation ,NSOperationQueue.NSOperati ...
- iOS的launch image --备用
当我们打开一款应用程序的时候,首先映入眼帘的往往并不是程序的主界面,而是经过精心设计的欢迎界面,这个界面通常会停留几秒钟,然后消失.看似很平常的一个小小的欢迎界面,其实还大有讲究. 一.为什么会出现欢 ...