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 区间限制费用流的更多相关文章

  1. codeforces gym 100357 I (费用流)

    题目大意 给出一个或与表达式,每个正变量和反变量最多出现一次,询问是否存在一种方案使得每个或式中有且仅有一个变量的值为1. 解题分析 将每个变量拆成三个点x,y,z. y表示对应的正变量,z表示对应的 ...

  2. Codeforces Gym 100002 E "Evacuation Plan" 费用流

    "Evacuation Plan" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  3. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  4. 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 ...

  5. hdu-5988 Coding Contest(费用流)

    题目链接: Coding Contest Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Ot ...

  6. POJ2195 Going Home[费用流|二分图最大权匹配]

    Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22088   Accepted: 11155 Desc ...

  7. BZOJ3130: [Sdoi2013]费用流[最大流 实数二分]

    3130: [Sdoi2013]费用流 Time Limit: 10 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 960  Solved: 5 ...

  8. 洛谷 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 ...

  9. Codeforces 730I [费用流]

    /* 不要低头,不要放弃,不要气馁,不要慌张 题意: 给两行n个数,要求从第一行选取a个数,第二行选取b个数使得这些数加起来和最大. 限制条件是第一行选取了某个数的条件下,第二行不能选取对应位置的数. ...

随机推荐

  1. 最新 用友网络java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.用友网络等10家互联网公司的校招Offer,因为某些自身原因最终选择了用友网络.6.7月主要是做系统复习.项目复盘.Leet ...

  2. 用elasticsearchdump备份恢复数据

    1.安装elastic searchdump mkdir /data/nodejs cd /data/nodejs wget https://nodejs.org/dist/v10.16.2/node ...

  3. 关于 layer.open 动态赋值不了的问题

    前情: layer.open({ type:1, // 用的是默认的信息弹框 content: $('#test'), // 这里不用 $('#test').html(), 不然后面获取不了值 }); ...

  4. python的u,r,b分别什么意思?

      我们经常在python当中看到以下内容: print(u'hi\thi\thi') print(b'hi\thi\thi') print(r'hi\thi\thi') 在其他语言里没见过类似的,所 ...

  5. [转帖]linux各种IPC机制

    linux各种IPC机制 docker中的资源隔离,一种就是IPC的隔离.IPC是进程间通信. 下面的文章转载自https://blog.csdn.net/yyq_9623/article/detai ...

  6. nginx文件服务器搭建

    一.安装 (CentOS 7) yum install nginx -y 如果报错: [u3@L3 /]$ sudo yum install nginx -y Loaded plugins: fast ...

  7. HTTP用户认证、追加协议以及相关技术简单学习

    1. 用户身份认证 BASIC认证(基本认证): DIGEST(摘要认证): SSL客户端认证: FormBase认证(表单认证)常用: session和cookie 2. 基于HTTP的追加协议 A ...

  8. SpringBoot以WAR包部署遇到的坑---集合贴

    ⒈忽略tomcat的context-path 方式一: 停止tomcat服务,删除tomcat安装目录的webapps目录下的ROOT目录,将打成的WAR包重命名为ROOT.war,重启tomcat服 ...

  9. (十五)mybatis 逆向工程

    目录 为什么需要逆向工程 使用方法 如何读懂生成的代码 总结 为什么需要逆向工程 对于数据库中的那么多的表 ,基本的 CRUD 操作 ,以及 mybatis 需要使用的 接口.mapper ,这些工作 ...

  10. 剑指offer34:第一个只出现一次的字符的位置

    1 题目描述 在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写). 2 思路和方法 ch[ ...