poj 1015(dp)
2012-11-27 13:37
417 查看
看的解题报告。。http://blog.csdn.net/lyy289065406/article/details/6671105
View Code
View Code
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int dp[22][805]; int path[22][805]; int p[205],d[205],s[205],v[205]; int ans[205]; int n,m; bool can(int j,int k,int i) { while(j>0) { if(path[j][k]==i) return false; k-=v[path[j][k]]; j--; } return true; } int main() { int cas=1; while(scanf("%d%d",&n,&m)) { if(!n && !m) break; memset(dp,-1,sizeof(dp)); memset(path,0,sizeof(path)); for(int i=1;i<=n;i++) { scanf("%d%d",&p[i],&d[i]); s[i]=p[i]+d[i]; v[i]=p[i]-d[i]; } int fix=m*20; dp[0][fix]=0; for(int j=1;j<=m;j++) { for(int k=0;k<=2*fix;k++) { if(dp[j-1][k]>=0) { for(int i=1;i<=n;i++) { if(dp[j][k+v[i]] < dp[j-1][k] + s[i] && can(j-1,k,i)) { dp[j][k+v[i]]=dp[j-1][k]+s[i]; path[j][k+v[i]]=i; } } } } } int minv=0; for(int k=0;k<=fix;k++) { if(dp[m][fix+k]>=0 && dp[m][fix+k]>dp[m][fix-k]) { minv=fix+k; break; } else if(dp[m][fix-k]>=0) { minv=fix-k; break; } } printf("Jury #%d\n",cas++); printf("Best jury has value %d for prosecution and value %d for defence:\n",(dp[m][minv]+minv-fix)/2,(dp[m][minv]+fix-minv)/2); for(int j=m,k=minv;j>0;j--) { ans[j]=path[j][k]; k-=v[ans[j]]; } sort(ans+1,ans+m+1); for(int i=1;i<=m;i++) printf(" %d",ans[i]); puts("\n"); } return 0; }
相关文章推荐
- poj 1015 Jury Compromise(DP)
- poj1015--动态规划(DP)
- DP-POJ-1015-Jury Compromise
- POJ 1015 Jury Compromise (dp)
- POJ 1015 Jury Compromise(DP)
- poj 1015 dp(陪审团成员的选择)
- POJ1015-Jury Compromise 以及 uva 323正确二维DP解法
- 【dp】POJ 1015 Jury Compromise
- poj 1015 Jury Compromise 状态压缩DP(不压缩也行)
- poj 1015 Jury Compromise_dp
- POJ1015-Jury Compromise-dp
- poj - 1015 - Jury Compromise(dp)
- poj 1015 Jury Compromise_dp
- POJ 1015 Jury Compromise(双塔DP)
- POJ-1015(离散化DP)
- POJ 1015 Jury Compromise dp
- POJ 1015 Jury Compromise(双塔DP)
- POJ 1015 DP
- poj 1015 DP
- POJ 1015 Jury Compromise ---- DP