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. 51nod 1106 质数检测——Mr判素数

    质数检测一般都是根号n的写法 当然Mr判素数的方法可以实现log的复杂度2333 Mr判素数的话 我们根据费马小定理只要P是素数 那么另一个素数x 满足 x^P-1≡1(mod P) 同时 x^2%P ...

  2. 【BZOJ】1635: [Usaco2007 Jan]Tallest Cow 最高的牛

    [题意]n头牛,其中最高h.给定r组关系a和b,要求满足h[b]>=h[a]且a.b之间都小于min(h[a],h[b]),求第i头牛可能的最高高度. [算法]差分 [题解]容易发现r组关系只能 ...

  3. 【BZOJ】3779 重组病毒

    [算法]Link-Cut Tree+线段树(维护DFS序) [题解]整整三天……T_T 这篇题解比较资瓷:permui 这道题虽然树形态没有变化,但用lct写的原因在于把题目中的操作一进行了神转化:每 ...

  4. 【51NOD-0】1081 子段求和

    [算法]树状数组(区间和) [题解]记得开long long #include<cstdio> #include<cstring> #include<algorithm& ...

  5. JS练习题(左侧菜单下拉+好友选中)

    题一.左侧菜单下拉 做题思路:先做菜单和子菜单,把子菜单默认隐藏.再用JS调样式. <style type="text/css"> *{ margin:0px auto ...

  6. JS之document例题讲解1(两张表之间数据转移、日期时间选择、子菜单下拉、用div做下拉菜单、事件总结)

    作业一:两个列表之间数据从一个列表移动到另一个列表 <div style="width:600px; height:500px; margin-top:20px"> & ...

  7. NGINX: 返回大 JSON 数据不完整的问题

    说明: 内容全部来自 [ CSDN 金玮良 ] nginx 返回数据不完整的问题 当nginx 遇到大数据流时,会把数据先放在自己的缓冲区,然后一并发给客户端. 那如果这个结论成立, 那一次请求的数据 ...

  8. javascript 事件知识集锦

    1.事件委托极其应用 转载的链接:  http://www.webhek.com/event-delegate/#comments 2. 解析javascript事件机制 转载链接:    http: ...

  9. bootstrap-table设置某列序号自增

    col = [{ field: 'SerialNumber', title: '序号', formatter: function (value, row, index) { return index+ ...

  10. Fiddler抓取HTTPS协议

    HTTPS协议握手过程: 1,客户端明文请求,把自己支持的非对称加密算法(用于使用CA证书公钥加密计算生成协商密钥的随机数per_master).对称加密算法(用于以后使用协商密钥加密传输内容).验证 ...