Gym - 101492I 区间限制费用流
https://cn.vjudge.net/problem/Gym-101492I
如果用单个点代表每个区间 利用拆点来限制区间的流量的话 点是 n^2/2+m个 边是2*n^2条
但是这样会T
解法1:单纯形
单纯形套板可以过
#include <bits/stdc++.h>
#define N
using namespace std; typedef unsigned ui;
typedef long double dbl; const dbl eps = 1e-; ui n, m, c[]; dbl a[][], x[], z; int dcmp(dbl d) { return d < -eps ? - : d <= eps ? : ; } // u in, v out
void pivot(ui u, ui v) {
swap(c[n + u], c[v]);
// row u *= 1 / a[u][v]
dbl k = a[u][v]; a[u][v] = ;
for (ui j = ; j <= n; ++j) a[u][j] /= k;
for (ui i = ; i <= m; ++i) {
if (i == u || !dcmp(a[i][v])) continue;
k = a[i][v]; a[i][v] = ;
for (ui j = ; j <= n; ++j)
a[i][j] -= a[u][j] * k;
}
} bool init() {
for (ui i = ; i <= n; ++i) c[i] = i;
while () {
ui u = , v = ;
for (ui i = ; i <= m; ++i)
if (dcmp(a[i][]) == - && (!u || dcmp(a[u][] - a[i][]) == )) u = i;
if (!u) return ;
for (ui j = ; j <= n && !v; ++j)
if (dcmp(a[u][j]) == -) v = j;
if (!v) return ;
pivot(u, v);
}
} int simplex() {
if (!init()) return ;
else while () {
ui u = , v = ;
for (ui j = ; j <= n; ++j)
if (dcmp(a[][j]) == && (!v || a[][j] > a[][v])) v = j; if (!v) {
z = -a[][];
for (ui i = ; i <= m; ++i)
x[c[n + i]] = a[i][];
return ;
} dbl w = 1e20;
for (ui i = ; i <= m; ++i)
if (dcmp(a[i][v]) == &&
dcmp(w - a[i][] / a[i][v]) == ) {
w = a[i][] / a[i][v];
u = i;
}
if (!u) return ;
pivot(u, v);
}
} int main(void) {
ios::sync_with_stdio(); cin.tie();
#ifndef ONLINE_JUDGE
ifstream cin("1.in");
#endif
ui t;
cin >> n >> m;
for (ui j = ; j <= n; ++j) cin >> a[][j];
for (ui i = ; i <= m; ++i) {
int l, r; cin >> l >> r;
for (int j = l; j <= r; ++j)
a[i][j] = ;
cin >> a[i][];
} int res = simplex();
if (res == ) cout << "Infeasible" << endl;
else if (res == ) cout << "Unbounded" << endl;
else {
cout << (long long)z << endl;
}
return ;
}
解法2:网络流

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<string.h>
#include<string>
#include<stdlib.h>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
const int maxn=;
const int maxm=;
struct node
{
int t,f,c,next;
}e[maxm*];
int a[maxn];
int head[maxn],dis[maxn],vis[maxn];
int pre[maxn];
int cnt,s,t;
void add(int s,int t,int c,int f)
{
e[cnt].t=t;
e[cnt].c=c;
e[cnt].f=f;
e[cnt].next=head[s];
head[s]=cnt++;
e[cnt].t=s;
e[cnt].c=;
e[cnt].f=-f;
e[cnt].next=head[t];
head[t]=cnt++;
}
void intt()
{
cnt=;
s=;t=;
memset(head,-,sizeof(head));
}
int spfa()
{
queue<int >que;
for(int i=;i<maxn;i++)
{
dis[i]=inf;
vis[i]=;
pre[i]=-;
}
dis[s]=;
vis[s]=;
que.push(s);
while(que.size())
{
int u=que.front();
que.pop();
vis[u]=;
for(int i=head[u];i!=-;i=e[i].next)
{
int v=e[i].t;
if(e[i].c>&&dis[u]+e[i].f<dis[v])
{
dis[v]=dis[u]+e[i].f;
pre[v]=i;
if(vis[v]==)
{
vis[v]=;
que.push(v);
}
}
}
}
if(pre[t]==-)return ;
return ;
}
ll mincost()
{
int flow=;
ll cost=;
while(spfa())
{
int tf=inf;
for(int i=pre[t];i!=-;i=pre[e[i^].t])
tf=min(e[i].c,tf);
flow+=tf;
for(int i=pre[t];i!=-;i=pre[e[i^].t])
{
e[i].c-=tf;
e[i^].c+=tf;
cost+=1ll*e[i].f*tf;
}
}
return cost;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
memset(a,,sizeof(a));
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
intt();
for(int i=;i<=m;i++)
{
int u,v,k;
scanf("%d%d%d",&u,&v,&k);
add(u,v+,inf,k);
}
for(int i=n+;i>=;i--)
add(i,i-,inf,);
for(int i=n+;i>=;i--)
{
int temp=a[i]-a[i-];
if(temp<)
{
add(i,t,-temp,);
}
else add(s,i,temp,);
}
printf("%lld\n",mincost());
}
转载自https://blog.csdn.net/dhydye/article/details/80515359 不懂原理QAQ
Gym - 101492I 区间限制费用流的更多相关文章
- codeforces gym 100357 I (费用流)
题目大意 给出一个或与表达式,每个正变量和反变量最多出现一次,询问是否存在一种方案使得每个或式中有且仅有一个变量的值为1. 解题分析 将每个变量拆成三个点x,y,z. y表示对应的正变量,z表示对应的 ...
- Codeforces Gym 100002 E "Evacuation Plan" 费用流
"Evacuation Plan" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...
- Codeforces Gym 101190M Mole Tunnels - 费用流
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
- 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem I. Plugs and Sockets 费用流
Problem I. Plugs and Sockets 题目连接: http://www.codeforces.com/gym/100253 Description The Berland Regi ...
- hdu-5988 Coding Contest(费用流)
题目链接: Coding Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
- POJ2195 Going Home[费用流|二分图最大权匹配]
Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22088 Accepted: 11155 Desc ...
- BZOJ3130: [Sdoi2013]费用流[最大流 实数二分]
3130: [Sdoi2013]费用流 Time Limit: 10 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 960 Solved: 5 ...
- 洛谷 1004 dp或最大费用流
思路: dp方法: 设dp[i][j][k][l]为两条没有交叉的路径分别走到(i,j)和(k,l)处最大价值. 则转移方程为 dp[i][j][k][l]=max(dp[i-1][j][k-1][l ...
- Codeforces 730I [费用流]
/* 不要低头,不要放弃,不要气馁,不要慌张 题意: 给两行n个数,要求从第一行选取a个数,第二行选取b个数使得这些数加起来和最大. 限制条件是第一行选取了某个数的条件下,第二行不能选取对应位置的数. ...
随机推荐
- QFramework 使用指南 2020 (四):脚本生成(2)ViewController 与 ViewController 嵌套绑定
在上一篇,我们学习了,脚本生成的基本使用. 在这一篇,我们试着深入,聊聊脚本生成给我们带来的便利. 脚本生成的便利 首先,我们要知道,在 Unity 的游戏世界中都是以 GameObject 为单位的 ...
- ue-cli3 取消eslint校验代码
参考链接:https://www.cnblogs.com/sjie/p/9884362.html
- kibana 设置登录认证
kibana 设置登录认证 SlowGO 2018.11.21 14:56 字数 59 阅读 658评论 0喜欢 0 kibana 本身没有用户名密码的设置,可以使用 nginx 来实现. 步骤 (1 ...
- RabbitMQ 幂等性概念及业界主流解决方案
RabbitMQ 幂等性概念及业界主流解决方案 2019年01月24日 15:57:03 JAVA@架构 阅读数:506 一.什么是幂等性 可以参考数据库乐观锁机制,比如执行一条更新库存的 SQL ...
- python学习-7 条件语句 while循环 + 练习题
1.死循环 while 1 == 1: print('ok') 结果是一直循环 2.循环 count = 0 while count < 10: print(count) count = cou ...
- 【Trie】Immediate Decodability
[题目链接]: https://loj.ac/problem/10052 [题意]: 就是给一些串,是否存在两个串是相同前缀的. [题解] 模板题,不想解释了. [代码]: #include<c ...
- varnish CLI管理
命令:varnishadm [-t timeout] [-S secret_file] [-T address:port] [-n name] [command [...]] ./varnishadm ...
- MySQL 子查询(一)
源自MySQL 5.7 官方手册 13.2.10 Subquery Syntax 〇.MySQL子查询介绍 子查询指的是嵌套在某个语句中的SELECT语句. MySQL支持标准SQL所要求的所有子查询 ...
- 排序之希尔排序(JS)
希尔排序(Shell's Sort)是插入排序的一种又称“缩小增量排序”(Diminishing Increment Sort),是直接插入排序算法的一种更高效的改进版本.希尔排序是非稳定排序算法.该 ...
- winForm入门学习
Windows窗体 属性: name:对象的名称 windowsState:初始化窗体的大小,Normal,Minimized,Maximized StartPosition:窗体起始位置,Manua ...