Codeforces 313D Ilya and Roads【Dp+思维】

351 篇文章 2 订阅

D. Ilya and Roads
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Everything is great about Ilya's city, except the roads. The thing is, the only ZooVille road is represented as n holes in a row. We will consider the holes numbered from 1 to n, from left to right.

Ilya is really keep on helping his city. So, he wants to fix at least k holes (perharps he can fix more) on a single ZooVille road.

The city has m building companies, the i-th company needs ci money units to fix a road segment containing holes with numbers of at least li and at most ri. The companies in ZooVille are very greedy, so, if they fix a segment containing some already fixed holes, they do not decrease the price for fixing the segment.

Determine the minimum money Ilya will need to fix at least k holes.

Input

The first line contains three integers n, m, k (1 ≤ n ≤ 300, 1 ≤ m ≤ 105, 1 ≤ k ≤ n). The next m lines contain the companies' description. The i-th line contains three integers li, ri, ci (1 ≤ li ≤ ri ≤ n, 1 ≤ ci ≤ 109).

Output

Print a single integer — the minimum money Ilya needs to fix at least k holes.

If it is impossible to fix at least k holes, print -1.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

Examples
Input
10 4 6
7 9 11
6 9 13
7 7 7
3 5 6
Output
17
Input
10 7 1
3 4 15
8 9 8
5 6 8
9 10 6
1 4 2
1 4 10
8 10 13
Output
2
Input
10 1 9
5 10 14
Output
-1


题目大意:

给你N个洞,需要进行填补至少K个,现在有M个施工队,对于这M个施工队的元素有三个:

L,R,C.表示这个施工队只能对于区间【L,R】的洞进行填补,雇佣这个施工队就需要花费C单位的价钱。

问至少填补K个洞的最小花费。


思路:


显然直接贪心是不可取的,我们考虑Dp.对于Dp来讲,对于最终结果的无非有三种状态:

①位子

②填补了多少个洞

③花费为多少。

那么我们设定Dp【i】【j】表示为从左到右的Dp,到位子i,一共填补了j个洞口的最小花费。


那么状态转移过程我们可以O(n*k*m)去求,然而时间复杂度不允许;

所以我们要优化这个m.观察到数据范围N不大,所以我们考虑预处理出cost【i】【j】表示填补区间【i,j】的洞口的最小花费.

因为是线性的动态规划,所以预处理cost【i】【j】的时候没有必要O(m*n^2)去做,直接O(m*n*2)的以左右端点各作为起点和终点去画一段即可:



那么状态转移方程就可以O(n^3)的去转移:




注意实现代码的细节就没有别的难点了。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define ll __int64
struct node
{
    int x,y;
    ll w;
}a[100040];
ll cost[305][305];
ll dp[305][305];
const ll INF=100000000000000000;
int main()
{
    int n,m,kk;
    while(~scanf("%d%d%d",&n,&m,&kk))
    {
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<=n;j++)
            {
                cost[i][j]=INF;
                dp[i][j]=INF;
            }
        }
        for(int i=1;i<=m;i++)
        {
            int x,y,w;
            scanf("%d%d%I64d",&a[i].x,&a[i].y,&a[i].w);
            for(int j=a[i].x;j<=a[i].y;j++)
            {
                cost[j][a[i].y]=min(cost[j][a[i].y],a[i].w);
                cost[a[i].x][j]=min(cost[a[i].x][j],a[i].w);
            }
            cost[a[i].x][a[i].y]=min(cost[a[i].x][a[i].y],a[i].w);
        }
        dp[0][0]=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=i;j++)
            {
                dp[i][j]=min(dp[i][j],dp[i-1][j]);
                for(int k=1;k<=i;k++)
                {
                    if(j-(i-k+1)>=0)
                    {
                        dp[i][j]=min(dp[i][j],cost[k][i]+dp[k-1][j-(i-k+1)]);
                    }
                }
            }
        }
        if(dp[n][kk]==INF)printf("-1\n");
        else
        printf("%I64d\n",dp[n][kk]);
    }
}





购物商城项目采用PHP+mysql有以及html+css jq以及layer.js datatables bootstorap等插件等开发,采用了MVC模式,建立一个完善的电商系统,通过不同用户的不同需求,进行相应的调配和处理,提高对购买用户进行配置….zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值