HDU 1074The Highest Mark(状压DP)
来源:互联网 发布:协同过滤算法python包 编辑:程序博客网 时间:2024/05/22 14:04
坑
需要输出路径,方法是对每种状态保存当前最优的路径,这题还要求字典序,需要比较
#include<iostream>#include<cstdio>#include<set>#include<string>#include<string.h>#include<cstring>#include<vector>#include<map>#include<queue>#include<stack>#include<cctype>#include<algorithm>#include<sstream>#include<utility>#include<cmath>#include<functional>#define mt(a) memset(a,0,sizeof (a))#define fl(a,b,c) fill(a,b,c)#define SWAP(a,b,t) (t=a,a=b,b=t)#define inf 1000000000+7using namespace std;typedef long long ll;class mnode{public:int num;int ans[16];};mnode dp[40000];class node{public:string name;int dl, d;};int main(){int T;cin >> T;while (T--){mt(dp);int n;cin >> n;node a[16];for (int i = 0; i<n; i++){cin >> a[i].name >> a[i].dl >> a[i].d;}for (int i = 0; i < 40000; i++)dp[i].num = inf;dp[0].num = 0;for (int s = 0; s<(1 << n); s++){for (int i = 0; i<n; i++){if ((s & (1 << i))){int alltime = 0, cot = 0;for (int j = 0; j < n; j++){if (j == i)continue;if (s&(1 << j)){alltime += a[j].d;cot++;}}if (dp[s].num > dp[s ^ (1 << i)].num + max(0, alltime + a[i].d - a[i].dl)){dp[s].num = dp[s ^ (1 << i)].num + max(0, alltime+a[i].d-a[i].dl);for (int k = 0; k < cot; k++){dp[s].ans[k] = dp[s ^ (1 << i)].ans[k];}dp[s].ans[cot] = i;}else if (dp[s].num == dp[s ^ (1 << i)].num + max(0, alltime + a[i].d - a[i].dl)){int flag = 0;for (int k = 0; k < cot; k++){if (a[dp[s].ans[k]].name>a[dp[s ^ (1 << i)].ans[k]].name){for (; k < cot; k++)dp[s].ans[k] = dp[s ^ (1 << i)].ans[k];flag = 1;break;}}if (flag)dp[s].ans[cot] = i;}}}}int mixx = inf;cout << dp[(1 << n) - 1].num << endl;for (int i = 0; i<n ; i++){cout << a[dp[(1<<n)-1].ans[i]].name << "\n";}}return 0;}
0 0
- HDU 1074The Highest Mark(状压DP)
- hdu 5501 The Highest Mark
- 贪心+dp hdu5501 The Highest Mark
- HDU5501 The Highest Mark 贪心+DP
- HDU 5501:The Highest Mark 01背包
- hdu(5501)——The Highest Mark
- hdu 5501 The Highest Mark(贪心+背包)
- The Highest Mark hdu 5501 贪心+背包
- hdu5501 The Highest Mark
- hdu 5501 The Highest Mark(变形01背包)
- BC#59 The Highest Mark
- hdu5501 The Highest Mark 贪心+动态规划
- nyoj747蚂蚁的难题(三)&&hdoj5501The Highest Mark【dp】
- hdoj 5501 The Highest Mark 【贪心 + 0-1背包】
- HDOJ 5501The Highest Mark(贪心+动态规划)
- hdoj 5501 The Highest Mark 【贪心 + 0-1背包】
- hdoj 5501The Highest Mark【贪心+0-1背包】
- hdu 4344 Mark the Rope
- HDU 1257SDOI(贪心)
- 2016校招百度美团360机器学习岗面试经历
- HTML5上传图片及其相关知识点
- CodeForces 219D-H - Choosing Capital for Treeland-树DP
- python类、对象、方法、属性之类与对象笔记
- HDU 1074The Highest Mark(状压DP)
- JVM内存管理
- 收藏的一些播客地址
- BZOJ 1026
- Ruby Gem命令详解
- linux 下 tomcat的配置
- JavaScript的toString()和valueOf()区别到底是什么
- 最长不重复子串
- Problem A