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个数使得这些数加起来和最大. 限制条件是第一行选取了某个数的条件下,第二行不能选取对应位置的数. ...
随机推荐
- 思科S系列220系列交换机多个漏洞预警
补天漏洞响应平台近期监测思科官方发布了关于思科 S 系列 220 系列交换机的3个漏洞修复通告,其中包含2个高危漏洞,最高CVSS 3.0评分9.8. 更新时间 2019年 08月 09日 威胁目标 ...
- 洛谷 题解 P4158 【[SCOI2009]粉刷匠】
状态: dp[i][j][k][0/1]: 到达第i行时, 到达第j列时, 刷到第k次时, 这一格有没有刷对 转移 换一块木板时肯定要多刷一次 dp[i][j][k][0]=max(dp[i-1][m ...
- OLTP和 OLAP区别
联机事务处理OLTP(on-line transaction processing) 主要是执行基本日常的事务处理,比如数据库记录的增删查改.比如在银行的一笔交易记录,就是一个典型的事务. OLTP的 ...
- Proxy 和aop
Proxy 就是代理,意思就是 你不用去做,别人代替你去处理 先来个静态代理 public interface Hello { void say(String name); } 被代理类 public ...
- 1.5JdbcTmeplates、Jpa、Mybatis、beatlsql、Druid的使用
Spring boot 连接数据库整合 -- create table `account`DROP TABLE `account` IF EXISTSCREATE TABLE `account` ( ...
- BurpSuite 爆破网页后台登陆
由于 Burp Suite是由Java语言编写而成,所以你需要首先安装JAVA的运行环境,而Java自身的跨平台性,使得软件几乎可以在任何平台上使用.Burp Suite不像其他的自动化测试工具,它需 ...
- python 定时爬取内容并发送报告到指定邮箱
import requests import smtplib import schedule import time from bs4 import BeautifulSoup from email. ...
- How does a browser know which response belongs to which request?
Today I knows that the server never send a request to a client! It just make response~ So,if the bro ...
- NameValuePair 简单名称值对节点类型
/// <summary> /// 组装普通文本请求参数用于post请求 /// </summary> /// <param name="parameters& ...
- Win10怎么添加开机启动项?Win10添加开机自动运行软件三种方法
Win10管理开机启动项的方法相信大家已经非常熟悉,msconfig命令各系统都通用,那么很多用户发觉Win10和Win7 XP等系统不同,没有启动文件夹,那么我们怎么添加开机启动项呢?如晨软件或程序 ...