【UOJ#179】线性规划 单纯形
题目链接:
Solution
就是单纯形模板题,这篇博客就是存一下板子。
Code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define eps 1e-9
inline int read()
{
int x=0,f=1; char ch=getchar();
while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
} int N,M,id[11000];
double a[1100][1100],c[1100],b[1100],v,ans[1100];
inline void Pivot(int l,int e)
{
swap(id[e],id[N+l]); double r=a[l][e]; a[l][e]=1;
for (int i=1; i<=N; i++) a[l][i]/=r;
b[l]/=r; for (int i=1; i<=M; i++)
if (i!=l) {
r=a[i][e]; a[i][e]=0,b[i]-=r*b[l];
for (int j=1; j<=N; j++) a[i][j]-=r*a[l][j];
} r=c[e];
c[e]=0;
for (int i=1; i<=N; i++) c[i]-=r*a[l][i]; v+=r*b[l];
} inline int Simplex()
{
int l,e; double k;
while (1) {
l=e=0; k=-eps;
for (int i=1; i<=M; i++)
if (b[i]<k) k=b[i],l=i;
if (!l) break;
k=-eps;
for (int i=1; i<=N; i++)
if (a[l][i]<k && (!e || (rand()&1))) k=a[l][i],e=i;
if (!e) {puts("Infeasible"); return 1;}
Pivot(l,e);
}
while (1) {
for (int i=1; i<=N; i++) {
if (c[i]>eps) {e=i; break;}
if (i==N) {printf("%.8lf\n",v); return 0;}
}
double re=1e18; l=0; for (int i=1; i<=M; i++)
if (a[i][e]>eps && b[i]/a[i][e]<re)
re=b[i]/a[i][e],l=i; if (!l) {puts("Unbounded"); return 2;}
Pivot(l,e);
} } int main()
{
N=read(),M=read(); int type=read(); for (int i=1; i<=N; i++) scanf("%lf",&c[i]),id[i]=i;
for (int i=1; i<=M; i++) {
for (int j=1; j<=N; j++) scanf("%lf",&a[i][j]);
scanf("%lf",&b[i]);
} int flag=Simplex(); if (flag || !type) return 0; for (int i=1; i<=M; i++) ans[id[N+i]]=b[i];
for (int i=1; i<=N; i++) printf("%.8lf ",ans[i]); return 0;
}
【UOJ#179】线性规划 单纯形的更多相关文章
- UOJ.179.线性规划(单纯形)
题目链接 这写得还不错:http://www.cnblogs.com/zzqsblog/p/5457091.html 引入基变量\(x_{i+n}\),将约束\(\sum_{i=1}^m a_{ij} ...
- UOJ#179. 线性规划[模板]
传送门 http://uoj.ac/problem/179 震惊,博主竟然还不会线性规划! 单纯形实在学不会啊……背个板子当黑盒用…… 学(chao)了NanoApe dalao的板子 #includ ...
- UOJ#179. 线性规划(线性规划)
描述 提交 自定义测试 这是一道模板题. (这个题现在标程挂了..哪位哥哥愿意提供一下靠谱的标程呀?) 本题中你需要求解一个标准型线性规划: 有 nn 个实数变量 x1,x2,…,xnx1,x2,…, ...
- uoj#179 线性规划
这是一道模板题. 本题中你需要求解一个标准型线性规划: 有nn个实数变量x1,x2,⋯,xnx1,x2,⋯,xn和mm条约束,其中第ii条约束形如∑nj=1aijxj≤bi∑j=1naijxj≤bi. ...
- 【UOJ #179】线性规划 单纯形模板
http://uoj.ac/problem/179 终于写出来了单纯性算法的板子,抄的网上大爷的qwq 辅助线性规划找非基变量时要加个随机化才能A,我也不知道为什么,卡精度吗? 2017-3-6UPD ...
- 【UOJ 179】 #179. 线性规划 (单纯形法)
http://uoj.ac/problem/179 补充那一列修改方法: 对于第i行: $$xi=bi-\sum Aij*xj$$ $$=bi-\sum_{j!=e} Aij*xj-Aie*xe ...
- UVA 10498 Happiness(线性规划-单纯形)
Description Prof. Kaykobad has given Nasa the duty of buying some food for the ACM contestents. Nasa ...
- Note -「线性规划」学习笔记
\(\mathcal{Definition}\) 线性规划(Linear Programming, LP)形式上是对如下问题的描述: \[\operatorname{maximize}~~~~z= ...
- 线性规划VB求解
线性规划VB求解 Rem 定义动态数组 Dim a() As Single, c() As Single, b() As Single, cb() As Single Dim aa() As Sing ...
随机推荐
- JAVA-Servlet高级应用
会话只是指一段指定的时间间隔. 会话跟踪是维护用户状态(数据)的一种方式.它也被称为servlet中的会话管理. Http协议是一个无状态的,所以我们需要使用会话跟踪技术来维护用户状态. 每次用户请求 ...
- WINDOWS控制界面操作命令for WIN10
Windows系统:开始--运行--命令大全: cmd--------CMD命令提示符 cleanmgr-------垃圾整理 compmgmt.msc---计算机管理 conf----------- ...
- [转载]Frontend Knowledge Structure
https://github.com/JacksonTian/fks http://code.csdn.net/news/2819224 本文为大家整理了一系列关于JavaScript的常用工具,包括 ...
- 关于z-index这个层级的问题
z-index它可真是一个神奇的东西,它可以随意安排你的层的叠加问题. 如你想让红色矩形压在蓝色矩形上,正常布局先建立一个红色的再建议一个蓝色的,就可以了. 但如果我相反来建立,那么就得借助z-ind ...
- 20155321 2016-2017-2 《Java程序设计》第五周学习总结
20155321 2016-2017-2 <Java程序设计>第五周学习总结 教材学习内容总结 第八章 异常处理 Java提供特有的语句进行处理 try { 需要被检测的代码; } cat ...
- 无法执行该操作,因为链接服务器 "xxxxx" 的 OLE DB 访问接口 "SQLNCLI" 无法启动分布式事务
在存储过程中使用事务,并且使用链接服务器时,报类似下面的错误 链接服务器"****"的 OLE DB 访问接口 "SQLNCLI10" 返回了消息 " ...
- mybatis介绍——(一)
官方API:http://www.mybatis.org/mybatis-3/index.html 中文: http://www.mybatis.org/mybatis-3/zh/index.html ...
- android休眠唤醒驱动流程分析【转】
转自:http://blog.csdn.net/hanmengaidudu/article/details/11777501 标准linux休眠过程: l power managemen ...
- Project Euler Problem2
Even Fibonacci numbers Problem 2 Each new term in the Fibonacci sequence is generated by adding the ...
- 一个无锁消息队列引发的血案(四)——月:RingQueue(上) 自旋锁
目录 (一)起因 (二)混合自旋锁 (三)q3.h 与 RingBuffer (四)RingQueue(上) 自旋锁 (五)RingQueue(中) 休眠的艺术 (六)RingQueue(中) 休眠的 ...