Codeforces1146G. \[Zoning Restrictions\]

在有高度限制和罚款的街道上规划建造房屋,目标是最大化收益。每个房屋高度对应不同收益,若违反限制则需支付罚款。通过使用最小割算法计算最大可能利润。
摘要由CSDN通过智能技术生成

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元。问最大收益
初始不计限制的时候最大的贡献自然是 n × h × h n\times h\times h n×h×h
但还要考虑损失
考虑最小割来求损失
设一个超级源S和超级汇T
将每一个点拆成0~h-1即h个点
将0点与S连一条正无穷的边
随后对于这h个点:

  • 第i个点与第i+1个点之间连一条 h × h − i × i h\times h-i\times i h×hi×i的边,表示损失

割掉第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],cur[N];
void make(int x,int y,int z)
{
    go[++len]=y;to[len]=last[x];w[len]=z;last[x]=cur[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])
    {
        last[x]=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;
        }
    }
    last[x]=cur[x];
    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、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值