DAG上的dp

因为本身升序就是拓扑序,所以建出图来直接从1到ndp即可,设f[i][j]为到i花费了j

#include<iostream>
#include<cstdio>
using namespace std;
const int N=1005,inf=1e9+7;
int n,m,b,h[N],cnt,f[N][N],ans=-inf;
struct qwe
{
int ne,to,va,c;
}e[N*10];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
void add(int u,int v,int w,int c)
{
cnt++;
e[cnt].ne=h[u];
e[cnt].to=v;
e[cnt].va=w;
e[cnt].c=c;
h[u]=cnt;
}
int main()
{
n=read()+1,m=read(),b=read();
for(int i=1;i<=m;i++)
{
int x=read(),y=read(),c=read(),z=read();
add(x+1,x+1+y,z,c);
}
for(int i=1;i<=n;i++)
for(int j=0;j<=b;j++)
f[i][j]=-inf;
f[1][0]=0;
for(int u=1;u<=n;u++)
for(int i=h[u];i;i=e[i].ne)
for(int j=e[i].va;j<=b;j++)
f[e[i].to][j]=max(f[e[i].to][j],f[u][j-e[i].va]+e[i].c);
for(int i=0;i<=b;i++)
ans=max(ans,f[n][i]);
printf("%d\n",ans<0?-1:ans);
return 0;
}

bzoj 1649: [Usaco2006 Dec]Cow Roller Coaster【dp】的更多相关文章

  1. BZOJ 1649: [Usaco2006 Dec]Cow Roller Coaster( dp )

    有点类似背包 , 就是那样子搞... --------------------------------------------------------------------------------- ...

  2. 【BZOJ】1649: [Usaco2006 Dec]Cow Roller Coaster(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1649 又是题解... 设f[i][j]表示费用i长度j得到的最大乐趣 f[i][end[a]]=ma ...

  3. BZOJ——1649: [Usaco2006 Dec]Cow Roller Coaster

    http://www.lydsy.com/JudgeOnline/problem.php?id=1649 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 7 ...

  4. bzoj1649 [Usaco2006 Dec]Cow Roller Coaster

    Description The cows are building a roller coaster! They want your help to design as fun a roller co ...

  5. 【动态规划】bzoj1649 [Usaco2006 Dec]Cow Roller Coaster

    很像背包. 这种在一个数轴上进行操作的题常常需要对区间排序. f[i][j]表示距离到i时,花费为j时的权值之和. f[x[i]+l[i]][j+c[i]]=max{f[x[i]][j]+w[i]}( ...

  6. Bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 深搜,bitset

    1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 554  Solved: 346[ ...

  7. BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐( dfs )

    直接从每个奶牛所在的farm dfs , 然后算一下.. ----------------------------------------------------------------------- ...

  8. bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐【dfs】

    从每个奶牛所在草场dfs,把沿途dfs到的草场的con都+1,最后符合条件的草场就是con==k的,扫一遍统计一下即可 #include<iostream> #include<cst ...

  9. BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐

    Description The cows are having a picnic! Each of Farmer John's K (1 <= K <= 100) cows is graz ...

随机推荐

  1. nyoj 86 找球号(一)(set,map)

    找球号(一) 时间限制:3000 ms  |            内存限制:65535 KB 难度:3   描述 在某一国度里流行着一种游戏.游戏规则为:在一堆球中,每个球上都有一个整数编号i(0& ...

  2. 567. Permutation in String

    Problem statement: Given two strings s1 and s2, write a function to return true if s2 contains the p ...

  3. 不同app之间activity的跳转

    关键在于指定activity的action属性 <activity android:name=".HardActivity"> <intent-filter> ...

  4. 交互设计:隐藏或显示大段文本的UI组件有哪些?

    应用场景: 在手机上要给列表中的每一项添加一个大段的介绍,应该用什么UI组件 A: 这里可以用,模态对话框,弹出提示,工具提示这类组件.模态对话框的好处,就是用关闭的按钮,用户操作方便:而弹出提示和工 ...

  5. Organize Your Train part II 字典树(此题专卡STL)

    Organize Your Train part II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8787   Acce ...

  6. World is Exploding 树状数组+离散化

    Given a sequence A with length n,count how many quadruple (a,b,c,d) satisfies: a≠b≠c≠d,1≤a<b≤n,1≤ ...

  7. POJ2586 Y2K Accounting Bug 解题报告

    Description Accounting for Computer Machinists (ACM) has sufferred from the Y2K bug and lost some vi ...

  8. 洛谷—— P3370 【模板】字符串哈希

    P3370 [模板]字符串哈希 题目描述 如题,给定N个字符串(第i个字符串长度为Mi,字符串内包含数字.大小写字母,大小写敏感),请求出N个字符串中共有多少个不同的字符串. 友情提醒:如果真的想好好 ...

  9. 学习日常笔记<day10>servlet编程

    1 如何开发一个Servlet 1.1 步骤: 1)编写java类,继承HttpServlet类 2)重新doGet和doPost方法 3)Servlet程序交给tomcat服务器运行!! 3.1 s ...

  10. IDUtil 永不重复的ID

    package com.xxx.common.util; import java.util.Random; /** * 各种id生成策略 * * @version 1.0 */ public clas ...