http://uoj.ac/problem/179

终于写出来了单纯性算法的板子,抄的网上大爷的qwq

辅助线性规划找非基变量时要加个随机化才能A,我也不知道为什么,卡精度吗?

2017-3-6UPD:问了网上的大爷,知道是防止被卡时间(因为单纯形的复杂度是指数级的)。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int N = 23;
const double eps = 1e-8; double a[N][N], ans[N];
int n, m, t, id[N << 1]; void pivot(int l, int e) {
swap(id[e], id[n + l]);
double r = a[l][e]; a[l][e] = 1;
for (int j = 0; j <= n; ++j)
a[l][j] /= r;
for (int i = 0; i <= m; ++i)
if (i != l) {
r = a[i][e]; a[i][e] = 0;
for (int j = 0; j <= n; ++j)
a[i][j] -= r * a[l][j];
}
} int main() {
scanf("%d%d%d", &n, &m, &t);
int i, j, l, e; double k, kk;
for (j = 1; j <= n; ++j) scanf("%lf", &a[0][j]), id[j] = j;
for (i = 1; i <= m; ++i) {
for (j = 1; j <= n; ++j)
scanf("%lf", &a[i][j]);
scanf("%lf", &a[i][0]);
} while (true) {
l = e = 0; k = -eps;
for (i = 1; i <= m; ++i)
if (a[i][0] < k) {
k = a[i][0];
l = i;
}
if (!l) break;
k = -eps;
for (j = 1; j <= n; ++j)
if (a[l][j] < k && (!e || (rand() & 1))) {
k = a[l][j];
e = j;
}
if (!e) {puts("Infeasible"); return 0;}
pivot(l, e);
} while (true) {
for (j = 1; j <= n; ++j)
if (a[0][j] > eps)
break;
if ((e = j) > n) break;
k = 1e18; l = 0;
for (i = 1; i <= m; ++i)
if (a[i][e] > eps && (kk = (a[i][0] / a[i][e])) < k) {
k = kk;
l = i;
}
if (!l) {puts("Unbounded"); return 0;}
pivot(l, e);
} printf("%.10lf\n", -a[0][0]);
if (!t) return 0;
for (i = 1; i <= m; ++i) ans[id[n + i]] = a[i][0];
for (i = 1; i <= n; ++i) printf("%.10lf ", ans[i]);
return 0;
}

【UOJ #179】线性规划 单纯形模板的更多相关文章

  1. UOJ.179.线性规划(单纯形)

    题目链接 这写得还不错:http://www.cnblogs.com/zzqsblog/p/5457091.html 引入基变量\(x_{i+n}\),将约束\(\sum_{i=1}^m a_{ij} ...

  2. UOJ#179. 线性规划[模板]

    传送门 http://uoj.ac/problem/179 震惊,博主竟然还不会线性规划! 单纯形实在学不会啊……背个板子当黑盒用…… 学(chao)了NanoApe dalao的板子 #includ ...

  3. UOJ#179. 线性规划(线性规划)

    描述 提交 自定义测试 这是一道模板题. (这个题现在标程挂了..哪位哥哥愿意提供一下靠谱的标程呀?) 本题中你需要求解一个标准型线性规划: 有 nn 个实数变量 x1,x2,…,xnx1,x2,…, ...

  4. uoj#179 线性规划

    这是一道模板题. 本题中你需要求解一个标准型线性规划: 有nn个实数变量x1,x2,⋯,xnx1,x2,⋯,xn和mm条约束,其中第ii条约束形如∑nj=1aijxj≤bi∑j=1naijxj≤bi. ...

  5. 【UOJ#179】线性规划 单纯形

    题目链接: http://uoj.ac/problem/179 Solution 就是单纯形模板题,这篇博客就是存一下板子. Code #include<iostream> #includ ...

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

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

  7. 【UOJ 179】 #179. 线性规划 (单纯形法)

    http://uoj.ac/problem/179 补充那一列修改方法: 对于第i行: $$xi=bi-\sum Aij*xj$$    $$=bi-\sum_{j!=e} Aij*xj-Aie*xe ...

  8. UOJ #35. 后缀排序 后缀数组 模板

    http://uoj.ac/problem/35 模板题,重新理了一遍关系.看注释吧.充分理解了倍增的意义,翻倍之后对上一次排序的利用是通过一种类似于队列的方式完成的. #include<ios ...

  9. 【UOJ179】线性规划(单纯形)

    题意: 思路:单纯形模板 ..,..]of double; idx,idy,q:..]of longint; c:..]of double; n,m,i,j,op,x,y:longint; eps,m ...

随机推荐

  1. UIImageView与UIScrollView的关系图

        UIImageView与UIScrollView的关系图           https://www.evernote.com/shard/s227/sh/0af9f23c-08e6-4be6 ...

  2. java servlet jsp 导入boostrap css js

    1.在导入boostrap.css的时候注意了 一定要注意路径,你知道把js和css包放在servlet服务器的静态路径下面就是 lib 文件夹路径下,直接使用 lib/js/boostrap.css ...

  3. idea 导入 java json 包

    1.java 项目导包 找到 External Libraries 下面的java版本包,在点击鼠标右键.直接找到jar路径全部选中导入即可.

  4. FileReader 与canvas结合使用显示图片

    话不多少,直接上代码 function fileChange() { var file = this.files[0]; var imageType = /^image\//; //是否是图片 if ...

  5. window对象的方法和属性汇总

    window对象有以下方法: open close alert confirm prompt setTimeout clearTimeout setInterval clearInterval mov ...

  6. 变量对象vs活动对象

    这是我见过描述的最为详尽的关于变量对象.活动对象以及闭包的解析,来自知乎,感谢答主: 作者:闭家锁链接:https://www.zhihu.com/question/36393048/answer/7 ...

  7. 火狐浏览器下点击a标签时出现虚线的解决方案

    1.兼容性问题 火狐浏览器下点击a标签时出现虚线 2.解决方案 a:focus { outline: none;}

  8. 【R语言学习】时间序列

    时序分析会用到的函数 函数 程序包 用途 ts() stats 生成时序对象 plot() graphics 画出时间序列的折线图 start() stats 返回时间序列的开始时间 end() st ...

  9. 【DeepLearning学习笔记】Coursera课程《Neural Networks and Deep Learning》——Week1 Introduction to deep learning课堂笔记

    Coursera课程<Neural Networks and Deep Learning> deeplearning.ai Week1 Introduction to deep learn ...

  10. Django rest framework 版本控制(源码分析)

    基于上述分析 #2.处理版本信息 处理认证信息 处理权限信息 对用户的访问频率进行限制 self.initial(request, *args, **kwargs) #2.1处理版本信息 #versi ...