数学(线性规划):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!小伙伴! 非常感谢您阅读海轰的文 ...
随机推荐
- Sql Server 中事务(begin tran/commit tran/rollback tran)的用法
ALTER PROCEDURE [dbo].[Proc_Test_commit1] @result int output, --成功 1; 失败 0 @message nvarchar ...
- ssh登录很慢解决方法
使用ssh客户端(如:putty)连接Linux服务器,可能会等待10-30秒才有提示输入密码.严重影响工作效率.登录很慢,登录上去后速度正常,这种情况主要有两种可能的原因: 1. DNS反向解析问题 ...
- 10.8 noip模拟试题
1.花 (flower.cpp/c/pas) [问题描述] 商店里出售n种不同品种的花.为了装饰桌面,你打算买m支花回家.你觉得放两支一样的花很难看,因此每种品种的花最多买1支.求总共有几种不同的 ...
- ASP.NET MVC 第五回 ActionResult的其它返回值
我们上边所看到的Action都是return View();我们可以看作这个返回值用于解析一个aspx文件.而它的返回类型是ActionResult如 public ActionResult Inde ...
- Initializer block.
Ref: Initializing Fields Instance initializers are permitted to refer to the current object via the ...
- android SDK 代理配置(东北大学)
启动 Android SDK Manager ,打开主界面,依次选择「Tools」.「Options...」,弹出『Android SDK Manager - Settings』窗口: 在『Andro ...
- Datatables+Bootstrap
http://sandbox.runjs.cn/show/thwac3ec 运行效果 <!DOCTYPE html> <html lang="en"> &l ...
- redhat6.4 配置centos6 yum替换
1.卸载掉系统redhat自带的yum rpm -qa |grep yum |xargs rpm -e --nodeps 2 下载相关的centos yum插件 主要有python-inipa ...
- 子元素的margin-top影响父元素原因和解决办法
这个问题会出现在所有浏览器当中,原因是css2.1盒子模型中规定, In this specification, the expression collapsing margins means tha ...
- C#遍历所有的Textbox控件并赋值为String.Empty
foreach (Control control in this.Controls) { if (control.GetType().Name.Equals("TextBox")) ...