uva 437 The Tower of Babylon
来源:互联网 发布:virtualbox怎么用软件 编辑:程序博客网 时间:2024/06/06 21:04
第一次写dp的题目,感觉代码写的很拙劣,虽然一次就AC了。
思路是DAG上的有向无环最长路,用h存放高度,然后往下所搜索就可以了。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;struct node{ int x,y,z;}node[100];int n;int G[200][200];int h[200];int vis[200];int k;int dp(int i){ if(vis[i]) return h[i]; vis[i]=1; for(int j=0;j<k;j++) if(G[i][j]) {h[i]=max(h[i],dp(j)+node[i].z);} return h[i];}int main(){ int kase=0; while(cin>>n&&n) { memset(G,0,sizeof(G)); memset(vis,0,sizeof(vis)); int a,b,c; k=0; int p=n; while(p--) { cin>>a>>b>>c; node[k].x=a;node[k].y=b;node[k].z=c;h[k]=c; k++; if(c!=b) {node[k].x=a;node[k].y=c;node[k].z=b;h[k]=b; k++;} if(a!=b) {node[k].x=b;node[k].y=a;node[k].z=c;h[k]=c; k++;} if(a!=b&&a!=c) {node[k].x=b;node[k].y=c;node[k].z=a;h[k]=a; k++;} if(c!=a) {node[k].x=c;node[k].y=b;node[k].z=a;h[k]=a; k++;} if(c!=a&&a!=b) {node[k].x=c;node[k].y=a;node[k].z=b;h[k]=b; k++;} } for(int i=0;i<k;i++) { for(int j=0;j<k;j++) { if(node[i].x<node[j].x&&node[i].y<node[j].y) G[i][j]=1; } } cout<<"Case "<<++kase<<": maximum height = "; int ans=0; for(int i=0;i<k;i++) ans=max(ans,dp(i)); cout<<ans<<endl; }}
0 0
- uva 437 The Tower of Babylon
- UVa 437 - The Tower of Babylon
- UVA 437 The Tower of Babylon
- UVa 437 - The Tower of Babylon LIS
- uva 437 - The Tower of Babylon
- UVa 437 - The Tower of Babylon
- UVa 437 - The Tower of Babylon
- UVa 437 - The Tower of Babylon
- UVa 437 - The Tower of Babylon
- UVA 437 The Tower of Babylon
- UVa:437 The Tower of Babylon
- UVA - 437 The Tower of Babylon
- Uva-437 The Tower of Babylon DP
- uva 437 The Tower of Babylon
- UVA 437 The Tower of Babylon
- UVA 437 The Tower of Babylon | dp
- UVA 437 The Tower of Babylon
- UVA 437 The Tower of Babylon
- 第35课:Kafka源码解读中分区数、Consumer并行度等
- Redis学习笔记(3)-Redis事务,过期时间,队列
- OpenCV学习笔记11(摄像机帧率实时显示,控制台)
- UNIX/Linux 下文件类型及权限
- CentOS6.5设置完SSH免密码登录之后仍然要求输入密码的问题
- uva 437 The Tower of Babylon
- 【编程马拉松】【020-小世界现象】
- vector的基本操作
- java.lang.RuntimeException: Unable to instantiate activity ComponentInfo异常
- 学习Java需要知道的事情
- 计算机文化历史科技
- 1023 ProblemW
- ajax跨域getjsonp
- [UER #1C]Dzy Loves Graph