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

来源:互联网 发布:linux找不到mysql命令 编辑:程序博客网 时间:2024/06/14 04:49

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 asn holes in a row. We will consider the holes numbered from 1 ton, 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 leastli and at mostri. 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 nextm lines contain the companies' description. Thei-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 thecin, cout streams or the%I64d specifier.

Examples
Input
10 4 67 9 116 9 137 7 73 5 6
Output
17
Input
10 7 13 4 158 9 85 6 89 10 61 4 21 4 108 10 13
Output
2
Input
10 1 95 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 __int64struct 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]);    }}





原创粉丝点击