【Solution】

接上一篇,在处理有向无环图的最长链问题的时候,可以在做拓扑排序的同时,一边做DP;

设f[i]表示第i个方块作为最上面的最高值;

f[y]=max(f[y],f[x]+h[y]);(x−>y)∈E

这样可以保证,按阶段进行DP,每次在获取f[x]的时候,你可以保证f[x]已经获得了;

最后取max(f[1..n])

【Code】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("D:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 30; struct abc{
LL c,k,g;
}; int n,b[4],nn,du[N*3+100];
LL dp[N*3+100];
abc a[N*3+100];
vector <int> G[N*3+100];
queue <int> dl; int main()
{
//Open();
int kk = 0;
while (~scanf("%d",&n) && n){
kk++;
ms(dp,-1);nn = 0;ms(du,0);
rep1(i,1,N*3) G[i].clear();
rep1(i,1,n){
rep1(j,1,3)
scanf("%d",&b[j]);
sort(b+1,b+1+3);
rep1(j,1,3){
nn++;
rep2(k,3,1)
if (k!=j){
a[nn].c = b[k];
break;
}
rep1(k,1,3)
if (k!=j){
a[nn].k = b[k];
break;
}
a[nn].g = b[j];
}
}
n = nn;
rep1(i,1,n)
rep1(j,1,n)
if (a[i].c > a[j].c && a[i].k > a[j].k){
G[i].pb(j);
du[j]++;
}
while (!dl.empty()) dl.pop();
rep1(i,1,n)
if (du[i]==0){
dl.push(i);
dp[i] = a[i].g;
du[i] = -1;
}
while (!dl.empty()){
int x = dl.front();
dl.pop();
int len = G[x].size();
rep1(i,0,len-1){
int y = G[x][i];
if (dp[y]==-1){
dp[y] = dp[x] + a[y].g;
}else
dp[y] = max(dp[y],dp[x]+a[y].g);
du[y]--;
if (du[y]==0){
dl.push(y);
du[y]= -1;
}
}
}
LL d = 0;
rep1(i,1,n)
d = max(d,dp[i]);
printf("Case %d: maximum height = ",kk);
printf("%lld\n",d);
}
return 0;
}

最新文章

  1. 精通css 高级web标准解决方案——可视化格式模型-定位模型
  2. JSP显示不完全问题
  3. 对JSP和Servlet性能优化,提升执行效率
  4. html a标签链接使用action 参数传递中文乱码
  5. Sqlserver Sql Agent Job 只能同时有一个实例运行
  6. Memcache 问题集锦
  7. android学习记录(十三)Task 和 Activity 回退栈操作。
  8. 英特尔实感3D摄像头
  9. 用bootstrap的tab插件做一个图层切换效果(感觉会误导淫们,大家当乐子看吧)
  10. java集合(2)- java中HashMap详解
  11. 汇编语言2(mooc)
  12. emqtt 试用(九)ssl认证 - 客户端 mqttfx 验证
  13. python实现常见排序算法
  14. DeepLearning.ai学习笔记(五)序列模型 -- week2 序列模型和注意力机制
  15. Redis(1)---五种数据结构
  16. HDU 3336 Count the string(next数组运用)
  17. vi中跳到文件的第一行和最后一行
  18. 洛谷P4139 上帝与集合的正确用法 [扩展欧拉定理]
  19. 拼接index
  20. 数据结构(二) 树Tree

热门文章

  1. php获取js里的参数
  2. 我的Java历程_Java对象类型的转换
  3. Hadoop_HDFS-基础知识摘要
  4. eclipse历史版本下载地址
  5. selenium自动化(二).........................................Demo篇
  6. 数组名作为函数参数以及sizeof用法
  7. Qt之QSpacerItem
  8. android -- 小问题 关于ListView设置了OnScrollListener之后onScrollStateChanged()和onScroll方法监听不到的问题
  9. JConsole远程监控Tomcat7
  10. Hadoop入门进阶步步高(二)-文件夹介绍