UVA103动态规划之DAG上的最长路及其字典序
2016-04-11 14:45
211 查看
小白书上讲的很清楚,因为没有起点和终点,所以这题用记忆化搜索。
代码:
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<algorithm> #include<string> #include<map> #include<queue> #include<stack> #include<set> #include<vector> #define LL long long using namespace std; const int maxn=1e4+10; const double eps=1e-6; int k,n; int a[40][20]; int b[40][40]; int dp[40]; int ddp(int i) { int &ans=dp[i]; if(ans) return ans; ans=1; for(int j=0;j<k;j++) { if(b[i][j]) ans=max(ans,ddp(j)+1); } return ans; } void init(int k,int n) { memset(b,0,sizeof(b)); for(int i=0;i<k;i++) { for(int j=0;j<k;j++) { int flag=1; for(int t=0;t<n;t++) { if(a[i][t]<=a[j][t]) { flag=0; break; } } if(flag) b[i][j]=1; } } } void print(int i) { for(int j=0;j<k;j++) if(b[i][j]&&dp[i]==dp[j]+1) { print(j); break; } cout<<i+1<<' '; } int main() { while(cin>>k>>n) { for(int i=0;i<k;i++) { for(int j=0;j<n;j++) scanf("%d",&a[i][j]); sort(a[i],a[i]+n); } init(k,n); cout<<endl; memset(dp,0,sizeof(dp)); for(int i=0;i<k;i++) dp[i]=ddp(i); int ans=0; int t; for(int i=0;i<k;i++) { if(dp[i]>ans) { t=i; ans=dp[i]; } } if(ans) { cout<<ans<<endl; print(t); cout<<endl; } else cout<<0<<endl; } return 0; }
相关文章推荐
- <css 十一>relative相对定位、absolute绝对定对
- github添加远程仓库报错:fatal: remote origin already exists.
- mysql数据库
- [C++日常小题] 三角螺旋数组
- 求1到10的和
- mysql中or和in的效率测试
- Android开发学习笔记:数据存取之Preference浅析
- DOM启蒙——第二章
- 通过栈帧来解释实参到形参的“似的单向传递”
- Realm for android Basic configuration and operation
- vim符号列表窗口
- 将一个数组编程0,1序列
- 关于CCNA的培训课程(4)-- 网络协议 路由器
- 【剑指offer系列】 翻转单词 VS 左旋字符串___42
- 用do-until语句求随机十个数的和
- /data/src/dragon/bidder_mod//src/proto_adapters/dragon_wax_adapter.h:11:对‘vtable for DragonWaxAdapter’未定义的引用
- 从零开始学github-1、github历史由来
- C++ 栈的应用
- 在SQL SERVER中批量替换字符串的方法
- memcache命令