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. 《Head First 设计模式》:外观模式

    正文 一.定义 外观模式提供了一个统一的接口,用来访问子系统中的一群接口.外观定义了一个高层接口,让子系统更容易使用. 要点: 外观模式将一个或数个类的复杂的一切都隐藏在背后,只显露出一个干净美好的外 ...

  2. 问题定位 | Peronca Xtrabackup 8.0近日踩坑总结 - xtrabackup 2.4和8.0区别

    目录 前言 适配过程中遇到的坑 xtrabackup 2.4和8.0区别 问题定位 坑一:MySQL 8.0 + Semi-Sync 重建问题 坑二:MySQL 8.0 + Group-Replica ...

  3. idea中展开折叠的文件夹

    1. 按键盘中的向右箭头 选中要展开的目录,按下键盘的向右箭头,这时候会展开一层当前目录.如果要展开所有,一直按住向右箭头不放. 左箭头则是折叠 2. 按数字键盘中的 * 选中要展开的目录,按下数字键 ...

  4. 焦大:SEO重思录(上)收录量和收录率的重新定位

    http://www.wocaoseo.com/thread-198-1-1.html 前一段焦大在seo前线就看到有人问为何我收录量很大但是流量很低呢?有时候几百万的收录量但是流量却只有1000不到 ...

  5. 使用tensorflow2识别4位验证码及思考总结

    在学习了CNN之后,自己想去做一个验证码识别,网上找了很多资料,杂七杂八的一大堆,但是好多是tf1写的,对tf1不太熟悉,有点看不懂,于是自己去摸索吧. 摸索的过程是异常艰难呀,一开始我直接用capt ...

  6. CefSharp如何判断页面是否加载完

    问题:CefSharp如何判断页面是否加载完毕. 摘要:相信C#用CefSharp做浏览器来发的应该有很多人都会有遇到这个问题.特别是要执行JavaScript的时候,涉及到跨页面的JavaScrip ...

  7. 总结删除文件或文件夹的7种方法-JAVA IO基础总结第4篇

    本文是Java IO总结系列篇的第4篇,前篇的访问地址如下: 总结java中创建并写文件的5种方式-JAVA IO基础总结第一篇 总结java从文件中读取数据的6种方法-JAVA IO基础总结第二篇 ...

  8. python小白入门基础(一:注释)

    # 注释:就是对代码的解释,方便大家阅读代码.注释后的代码程序不会执行.# 注释的分类:单行注释和多行注释# (1)单行注释# 在代码前面加个#字符print("hello world&qu ...

  9. java-对象引用和对象本身

    示意图 应用场景 1.赋值 只有用新的对象本身直接赋值给对应引用,该对象引用的值会改变,因为对象引用指向的对象本身改变了. 2.方法入参 调用方法和被调用方法. 代码 import com.aliba ...

  10. servlet web项目连接数据库报错

    报错信息类似这样: Wed May 27 14:15:54 CST 2020 WARN: Establishing SSL connection without server's identity v ...