Description

You are planning to build housing on a street. There are n spots available on the street on which you can build a house. The spots are labeled from 1 to n from left to right. In each spot, you can build a house with an integer height between 0 and h.

In each spot, if a house has height a, you can gain a2 dollars from it.

The city has m zoning restrictions though. The i-th restriction says that if the tallest house from spots li to ri is strictly more than xi, you must pay a fine of ci.

You would like to build houses to maximize your profit (sum of dollars gained minus fines). Determine the maximum profit possible.

Input

The first line contains three integers n,h,m (1≤n,h,m≤50) — the number of spots, the maximum height, and the number of restrictions, respectively.

Each of the next m lines contains four integers li,ri,xi,ci (1≤li≤ri≤n, 0≤xi≤h, 1≤ci≤5000).

Output

Print a single integer denoting the maximum profit you can make.

Solution

大意就是建房子,房子最大高度为h,对于高度为i的建筑,收益为i*i。现在有m个限制,对于第i个限制,在li~ri这段区间内,高度如果有超过xi的需要罚款ci元。问最大收益

考虑最小割来求损失

首先设一个超级源S和超级汇T

将每一个点拆成0~h-1即h个点

将0点与S连一条正无穷的边

随后对于这h个点:

  • 第i个点与第i+1个点之间连一条hh-ii的边,表示损失

割掉第i个点与第i+1个点之间的连边就代表选了第i个点

随后,对于每个限制

  • li~ri区间内的第xi个点与该点连一条正无穷的边,因为这条边不可能被割

    该点再与T连一条ci的边,表示损失

最后输出即可

(第一次打sap,之前都在打dinic呢)

#include <cstdio>
#include <algorithm>
#define inf 1000000007
#define S 4000
#define T 4001
#define M 20001
#define N 5001
#define open(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout);
#define id(x,y) (x-1)*(h+1)+y+1
using namespace std;
int len,n,h,m,l,r,x,c,cnt,ans,i,j,go[M],to[M],last[N],w[M],dis[N],gap[N];
void make(int x,int y,int z)
{
go[++len]=y;to[len]=last[x];w[len]=z;last[x]=len;
}
void add(int x,int y,int z)
{
make(x,y,z);
make(y,x,0);
}
int sap(int x,int flow)
{
if (x==T) return flow;
int tmp,have=flow;
for (int i=last[x];i;i=to[i])
{
if (w[i] && dis[x]==dis[go[i]]+1)
{
tmp=sap(go[i],min(have,w[i]));
w[i]-=tmp;w[i^1]+=tmp;have-=tmp;
if (!have) return flow;
}
}
if (!--gap[dis[x]]) dis[S]=T;
++gap[++dis[x]];
return flow-have;
}
int main()
{
open("restrictions");
scanf("%d%d%d",&n,&h,&m);
len=1;
for (i=1;i<=n;i++)
{
add(S,id(i,0),inf);
for (j=0;j<h;j++)
{
add(id(i,j),id(i,j+1),h*h-j*j);
}
}
cnt=id(n,h);
for (i=1;i<=m;i++)
{
scanf("%d%d%d%d",&l,&r,&x,&c);
if (x==h) continue;
add(++cnt,T,c);
for (j=l;j<=r;j++)
add(id(j,x+1),cnt,inf);
}
ans=n*h*h;
while (dis[S]<T)
ans-=sap(S,inf);
printf("%d",ans);
return 0;
}

Codeforces1146G. Zoning Restrictions的更多相关文章

  1. CF1146G Zoning Restrictions

    CF1146G Zoning Restrictions 网络流 h<=50? 直接都选择最大的,ans=n*h*h 最小割 考虑舍弃或者罚款 有一个>x就要罚款? 经典取值限制的模型:切糕 ...

  2. codeforces A. Zoning Restrictions Again

    A. Zoning Restrictions Again ou are planning to build housing on a street. There are n spots availab ...

  3. 【CF1146】Forethought Future Cup - Elimination Round

    Forethought Future Cup - Elimination Round 窝也不知道这是个啥比赛QwQ A. Love "A" 给你一个串,你可以删去若干个元素,使得最 ...

  4. CF集萃2

    CF1155D - Beautiful Array 题意:给你一个序列和x,你可以选择任意一个子串(可以为空)乘上x,使得得到的序列最大子串和最大.求这个最大值.30w,2s. 解:设fi,0/1/2 ...

  5. Hibernate的 Restrictions用法

    方法说明 方法 说明 Restrictions.eq = Restrictions.allEq 利用Map来进行多个等于的限制 Restrictions.gt > Restrictions.ge ...

  6. 【WebGoat习题解析】Parameter Tampering->Bypass HTML Field Restrictions

    The form below uses HTML form field restrictions. In order to pass this lesson, submit the form with ...

  7. 新浪微博授权失败:applications over the unaudited use restrictions

    在用新浪微博授权第三方app时,授权失败,log显示 com.sina.weibo.sdk.exception.WeiboHttpException: {,"request":&q ...

  8. Hibernate Criteria Restrictions

    HQL运算符 QBC运算符 含义 = Restrictions.eq() 等于equal <>  Restrictions.ne() 不等于not equal >  Restrict ...

  9. hibernate criteria中Restrictions的用法

    方法说明 方法 说明 Restrictions.eq = Restrictions.allEq 利用Map来进行多个等于的限制 Restrictions.gt > Restrictions.ge ...

随机推荐

  1. Linux 常用软件清单

    Linux 常用软件清单 下面是Linux环境的一些软件(有些只是关键字,直接搜素即可): arch 系列的是 pacman -Ss <关键字> debian 系列的是 apt searc ...

  2. 创建human用户登录数据库创建表

    根据人力资源管理系统中表的设计,创建human用户登录数据库创建 准备阶段 把运行脚本复制到D:\app\Administrator\product\11.2.0\dbhome_1\demo\sche ...

  3. DVWA之文件上传(二)

    <?php if( isset( $_POST[ 'Upload' ] ) ) { // Where are we going to be writing to? $target_path = ...

  4. Java高级特性——反射机制(完结)——反射与注解

    按照我们的学习进度,在前边我们讲过什么是注解以及注解如何定义,如果忘了,可以先回顾一下https://www.cnblogs.com/hgqin/p/13462051.html. 在学习反射和注解前, ...

  5. shazidouhuiapp

    在选择了软件工程专业之后,指导教师也让我们参加到了学长学姐的作业之中来,使用学长学姐们的软件并写出自己的使用评价以及自己的一些小评价. 我这次体验的是第三组的学长学姐们的软件,他们的队名叫天公疼憨仔, ...

  6. FJOI2020 游记

    Day -1 啥都不会,药丸 看了看统考题,好难,爆零的节奏 文化课OI双爆炸 尽力吧 Day 0 花三个多小时才到考场 福州真的好热 签到 在小礼堂待了一会,顺便给手机充了电 四点试机,今年用了新系 ...

  7. .net core 返回业务错误(不抛异常)

    在开始之前你需要知道: 1.通过抛异常--全局捕获异常的方式返回业务错误信息性能是非常差的(不知道为什么的可以百度一下) 2.如何将错误信息绑定到mvc模型验证中 自定义返回内容 //返回内容接口 p ...

  8. android 数据绑定(6)自定义绑定方法、双向数据绑定

    1.官方文档 https://developer.android.com/topic/libraries/data-binding/binding-adapters https://developer ...

  9. Activiti7 使用监听器分配任务人员

    视屏中老师说,一般没有人用但是我还是想试试 但是当我画图的时候,发现IDEA的那个listener监听器点不开,不知道是不是我下载的插件不对还是什么原因,所以就亲自写了,看看到时候不行就下载一个Ecl ...

  10. 关于取表中id最大值+1的select语句,哪种效率更高?

    需求:取stock表中id最大值+1,作为下一个id值. 特殊情况:考虑到表中会没有值,max(id)会返回空,因此需要用case when进行判断. 实现一:select (case max(id) ...