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;
}

最新文章

  1. python之路十三
  2. REST服务返回自定义的HttpResponseMessage
  3. Apache Thrift 环境配置
  4. VC ++ MFC activex 控件获取连接的VPN 信息
  5. 关于JDK中正则表达式
  6. Android支付接入(二):移动游戏基地
  7. 在RichTextBox控件中添加图片和文字
  8. jQuery手机对话框插件
  9. javascript 中arguments.callee 调用自身
  10. hdu 1233 还是畅通project(kruskal求最小生成树)
  11. 常用表单验证&amp;&amp;常用正则
  12. C# web项目利用docx文档作为模板~为打印专做的解决方案
  13. 【Python 25】52周存钱挑战5.0(datetime库和import)
  14. redis的pub/sub命令
  15. Elasticsearch 5.4.3实战--Java API调用:索引mapping创建
  16. Fiddler中session请求/响应类型与图标含义
  17. 联想电脑t450,t460p,t470等安装好ubuntu后启动找不到系统
  18. chrome播放m3u8視頻失败
  19. Ubuntu禁用触摸板
  20. 删除php数组中的元素

热门文章

  1. cinder-volume服务上报自己的状态给cinder-scheduler的rpc通信代码分析
  2. 第六篇Scrum冲刺博客--Interesting-Corps
  3. 在服务器上使用python-gym出现的显示问题
  4. h5c3
  5. 分享一个FileUtil工具类,基本满足web开发中的文件上传,单个文件下载,多个文件下载的需求
  6. frozenset冻结集合函数
  7. 【Nginx】如何基于主从模式搭建Nginx+Keepalived双机热备环境?这是最全的一篇了!!
  8. Laravel Facade原理及使用
  9. Android开发,java开发程序员常见面试题,求100-200之间的质数,java逻辑代码
  10. 【转】LeakCanary