题意

将N种食品分给m个参赛选手,一个单位的某食品给某个选手一定满足度,每个选手有一个最大满足度。为了避免浪费,分给每一个选手的食品都不超越选手的满足度。已知的各种食品的单价,求最多可以花的钱。

思路

标准的线性规划---直接求即可……目前只会用用模板,还没搞清楚单纯性算法……

代码

[cpp]
/**
Simplex C(n+m)(n)
maximize:
c[1]*x[1]+c[2]*x[2]+...+c[n]*x[n]+ans
subject to
a[1,1]*x[1]+a[1,2]*x[2]+...a[1,n]*x[n] <= rhs[1]
a[2,1]*x[1]+a[2,2]*x[2]+...a[2,n]*x[n] <= rhs[2]
......
a[m,1]*x[1]+a[m,2]*x[2]+...a[m,n]*x[n] <= rhs[m]
限制:
传入的矩阵必须是标准形式的, 即目标函数要最大化;约束不等式均为<= ;xi为非负数(>=0).
simplex返回参数:
OPTIMAL 有唯一最优解
UNBOUNDED 最优值无边界
FEASIBLE 有可行解
INFEASIBLE 无解
n为元素个数,m为约束个数
线性规划:
max c[]*x;
a[][]<=rhs[];
ans即为结果,x[]为一组解(最优解or可行解)
**/

const double eps = 1e-8;
const double inf = 1e15;

#define OPTIMAL -1 //表示有唯一的最优基本可行解
#define UNBOUNDED -2 //表示目标函数的最大值无边界
#define FEASIBLE -3 //表示有可行解
#define INFEASIBLE -4 //表示无解
#define PIVOT_OK 1 //还可以松弛
#define maxn 1000

struct LinearProgramming{
int basic[maxn], row[maxn], col[maxn];
double c0[maxn];

double dcmp(double x){
if (x > eps) return 1;
else if (x < -eps) return -1;
return 0;
}
void init(int n, int m, double c[], double a[maxn][maxn], double rhs[], double &ans) { //初始化
for(int i = 0; i <= n+m; i++) {
for(int j = 0; j <= n+m; j++) a[i][j]=0;
basic[i]=0; row[i]=0; col[i]=0;
c[i]=0; rhs[i]=0;
}
ans=0;
}
//转轴操作
int Pivot(int n, int m, double c[], double a[maxn][maxn], double rhs[], int &i, int &j){
double min = inf;
int k = -1;
for (j = 0; j <= n; j ++)
if (!basic[j] && dcmp(c[j]) > 0)
if (k < 0 || dcmp(c[j] - c[k]) > 0) k = j;
j = k;
if (k < 0) return OPTIMAL;
for (k = -1, i = 1; i <= m; i ++) if (dcmp(a[i][j]) > 0)
if (dcmp(rhs[i] / a[i][j] - min) < 0){ min = rhs[i]/a[i][j]; k = i; }
i = k;
if (k < 0) return UNBOUNDED;
else return PIVOT_OK;
}
int PhaseII(int n, int m, double c[], double a[maxn][maxn], double rhs[], double &ans, int PivotIndex){
int i, j, k, l; double tmp;
while(k = Pivot(n, m, c, a, rhs, i, j), k == PIVOT_OK || PivotIndex){
if (PivotIndex){ i = PivotIndex; j = PivotIndex = 0; }
basic[row[i]] = 0; col[row[i]] = 0; basic[j] = 1; col[j] = i; row[i] = j;
tmp = a[i][j];
for (k = 0; k <= n; k ++) a[i][k] /= tmp;
rhs[i] /= tmp;
for (k = 1; k <= m; k ++)
if (k != i && dcmp(a[k][j])){
tmp = -a[k][j];
for (l = 0; l <= n; l ++) a[k][l] += tmp*a[i][l];
rhs[k] += tmp*rhs[i];
}
tmp = -c[j];
for (l = 0; l <= n; l ++) c[l] += a[i][l]*tmp;
ans -= tmp * rhs[i];
}
return k;
}
int PhaseI(int n, int m, double c[], double a[maxn][maxn], double rhs[], double &ans){
int i, j, k = -1;
double tmp, min = 0, ans0 = 0;
for (i = 1; i <= m; i ++)
if (dcmp(rhs[i]-min) < 0){min = rhs[i]; k = i;}
if (k < 0) return FEASIBLE;
for (i = 1; i <= m; i ++) a[i][0] = -1;
for (j = 1; j <= n; j ++) c0[j] = 0;
c0[0] = -1;
PhaseII(n, m, c0, a, rhs, ans0, k);
if (dcmp(ans0) < 0) return INFEASIBLE;
for (i = 1; i <= m; i ++) a[i][0] = 0;
for (j = 1; j <= n; j ++)
if (dcmp(c[j]) && basic[j]){
tmp = c[j];
ans += rhs[col[j]] * tmp;
for (i = 0; i <= n; i ++) c[i] -= tmp*a[col[j]][i];
}
return FEASIBLE;
}
//standard form
//n:原变量个数 m:原约束条件个数
//c:目标函数系数向量-[1~n],c[0] = 0;
//a:约束条件系数矩阵-[1~m][1~n] rhs:约束条件不等式右边常数列向量-[1~m]
//ans:最优值 x:最优解||可行解向量-[1~n]
int simplex(int n, int m, double c[], double a[maxn][maxn], double rhs[], double &ans, double x[]){
int i, j, k;
//标准形式变松弛形式
for (i = 1; i <= m; i ++){
for (j = n+1; j <= n+m; j ++) a[i][j] = 0;
a[i][n+i] = 1; a[i][0] = 0;
row[i] = n+i; col[n+i] = i;
}
k = PhaseI(n+m, m, c, a, rhs, ans);
if (k == INFEASIBLE) return k;
k = PhaseII(n+m, m, c, a, rhs, ans, 0);
for (j = 0; j <= n+m; j ++) x[j] = 0;
for (i = 1; i <= m; i ++) x[row[i]] = rhs[i];
return k;
}
}ps; //Primal Simplex

int n,m;
double c[maxn], ans, a[maxn][maxn], b[maxn], x[maxn];

int main(){
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
while(scanf("%d %d", &n, &m) != EOF){
double ans;
ps.init(n, m, c, a, b, ans);
for (int i = 1; i <= n; i ++)
scanf("%lf", &c[i]);
for (int i = 1; i <= m; i ++){
for (int j = 1; j <= n; j ++){
scanf("%lf", &a[i][j]);
}
scanf("%lf", &b[i]);
}
ps.simplex(n,m,c,a,b,ans,x);
printf("Nasa can spend %.0f taka.\n", ceil(m*ans));
}
return 0;
}
[/cpp]

UVa 10498 Happiness! (线性规划)的更多相关文章

  1. UVA 10498 Happiness(线性规划-单纯形)

    Description Prof. Kaykobad has given Nasa the duty of buying some food for the ACM contestents. Nasa ...

  2. 数学(线性规划):UVAoj 10498 Happiness

    Problem GHappiness! Input: standard inputOutput: standard outputTime Limit: 3 seconds Prof. Kaykobad ...

  3. 【Uva 10498】满意值

    Description Kaykobad教授把为ACM选手买饭的任务交给了Nasa.Nasa决定买n种不同的食物.然后他询问了m名选手对每种食物的需求量.选手们当然不会给出任何符合逻辑的回答,他们只是 ...

  4. 算法模板の数学&数论

    1.求逆元 int inv(int a) { ) ; return (MOD - MOD / a) * inv(MOD % a); } 2.线性筛法 bool isPrime[MAXN]; int l ...

  5. UVA11722概率问题之线性规划

    链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&am ...

  6. BZOJ 2127: happiness [最小割]

    2127: happiness Time Limit: 51 Sec  Memory Limit: 259 MBSubmit: 1815  Solved: 878[Submit][Status][Di ...

  7. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  8. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  9. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

随机推荐

  1. Nagios 快速实现数据可视化的几种方式

    Nagios 是一款强大的开源监控软件,但他本身不能绘图,只能查看当前数据,不能看历史数据以及趋势,也正因此,想要更舒适的使用就要搭配绘图软件,现在可搭配的绘图软件有很多,例如 pnp4nagios, ...

  2. JavaScript 性能分析新工具 OneProfile

    OneProfile 是一个网页版的小工具,可以用全新的方式展示 JavaScript 性能分析的结果,帮助开发者洞悉函数调用关系,优化应用性能. 点击打开 OneProfile 背景 Chrome ...

  3. 在 Java EE 组件中使用 Camel Routes

    摘要:你可以通过集成 Camel 和 WildFly 应用服务器(使用 WildFly-Camel 子系统)在 Java EE 组件中开始使用 Apache Camel Routes. [编者按]作者 ...

  4. HDU 4022 Bombing(stl,map,multiset,iterater遍历)

    题目 参考了     1     2 #define _CRT_SECURE_NO_WARNINGS //用的是STL中的map 和 multiset 来做的,代码写起来比较简洁,也比较好容易理解. ...

  5. jQuery学习记录1

    jquery 和 js css里面都是坑呀 this.style.backgroundColor 和 css {background:#8df;} 是冲突的,用了前者,再$(this).addClas ...

  6. ****RESTful API 设计最佳实践(APP后端API设计参考典范)

    http://blog.jobbole.com/41233/ 背景 目前互联网上充斥着大量的关于RESTful API(为方便,下文中“RESTful API ”简写为“API”)如何设计的文章,然而 ...

  7. SqlBulkCopy大批量数据插入到sql表中

    alter TYPE TableType AS TABLE ( Name VARCHAR() , code VARCHAR() ) GO alter PROCEDURE usp_InsertProdu ...

  8. Spring学习总结(1)——Spring AOP的概念理解

    1.我所知道的aop 初看aop,上来就是一大堆术语,而且还有个拉风的名字,面向切面编程,都说是OOP的一种有益补充等等.一下子让你不知所措,心想着:怪不得 很多人都和我说aop多难多难 .当我看进去 ...

  9. [转]linux CentOS 安装 Nginx

    网上找的教程,一路走下来的,原文如下: 一.安装nginx     1.在nginx官方网站下载一个包,下载地址是:http://nginx.org/en/download.html     2.Wi ...

  10. React事件函数简介

    一.事件汇总 二.例子 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset= ...