【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中多个异常的捕获顺序(多个catch)
import java.io.IOException; public class ExceptionTryCatchTest { public void doSomething() throws IO ...
- 一个ssm综合小案例-商品订单管理----写在前面
学习了这么久,一直都是零零散散的,没有把知识串联起来综合运用一番 比如拦截器,全局异常处理,json 交互,RESTful 等,这些常见技术必须要掌握 接下来呢,我就打算通过这么一个综合案例把这段时间 ...
- 最好用的xshell替代软件----FinalShell工具
2017年8月份NetSarang公司旗下软件家族的官方版本被爆被植入后门着实让我们常用的Xshell,Xftp等工具火了一把,很长时间都是在用Xshell,不过最近发现了一款同类产品FinalShe ...
- AngularJS入门基础——表单验证
<form name="form" novalidata> <label name="email">your email</l ...
- 关于 xcode5 的no matching provisioning profiles found
CHENYILONG Blog 关于 xcode5 的no matching provisioning prof- about the question in xcode5 "no matc ...
- DVWA的Xss跨站总结
Xss跨站总结 初级防护的代码 Poc:<script>alert(1)</script> 上图防护的代码 为输入的结果就为输出的结果 中级防护的代码 Poc:<scri ...
- 恶意代码分析实战-确认EXE什么时候编译的
场景 确认开源的后门在中毒机器上是什么版本,具有什么功能. 思路 1.查看样本PE里的编译时间 2.对照开源后门里组件的编译时间 技术点 查看NT头-TimeDateStamp struct IMAG ...
- nginx tomcat 自动部署python脚本【转】
#!/usr/bin/env python #--coding:utf8-- import sys,subprocess,os,datetime,paramiko,re local_path='/ho ...
- MySQL用户密码过期登陆报错ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
今天接到主从复制失败告警,查看MySQL,发现MySQL能够登陆但是执行命令报错, ERROR 1820 (HY000): You must reset your password using ALT ...
- ftp配置文件
vsftpd配置文件采用“#”作为注释符,以“#”开头的行和空白行在解析时将被忽略,其余的行被视为配置命令行,每个配置命令的“=”两边不要留有空格.对于每个配置命令,在配置文件中还列出了相关的配置说明 ...