HDU 4152 ZZYs Dilemma(枚举 or dfs)
来源:互联网 发布:电脑qq多开器软件 编辑:程序博客网 时间:2024/06/03 17:02
ZZY’s Dilemma
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 648 Accepted Submission(s): 276
Problem Description
ZZY has many habits like seeing movie, listening music, playing PC game and football and so on. Besides he has many goals, as not every habit is good for achieving his goals, he falls into the dilemma of making a choice between his goals and habits.
Now ,we define the effect that one habit has on each goal is represented as a vector,and the elements of the vector are integers,ex.vector(100,90,-10,80)means it has 100 point effect on goal 1,90 point effect on goal 2,-10 point effect on goal 3 and 80 point effect on goal 4(the positive point means good effect and the negative point means bad effect),and the given requirement of each goal is represented as integer.please help ZZY to achieve his goals as well as keeps his habits as many as possible.
Now ,we define the effect that one habit has on each goal is represented as a vector,and the elements of the vector are integers,ex.vector(100,90,-10,80)means it has 100 point effect on goal 1,90 point effect on goal 2,-10 point effect on goal 3 and 80 point effect on goal 4(the positive point means good effect and the negative point means bad effect),and the given requirement of each goal is represented as integer.please help ZZY to achieve his goals as well as keeps his habits as many as possible.
Input
There are multi cases , read the data until EOF.( No more than 10 cases )
Line 1: The number of ZZY’s goals N(0<N<=20)
Line 2: The requirement of each goals (0 < w <= 1000)
Line 3: The number of ZZY’s habits M(0 < M <= 16)
Line 4-M+4: Each line contains N integers, the ith integer represents the effect on ith goal (-1000 <= data <= 1000).
Line 1: The number of ZZY’s goals N(0<N<=20)
Line 2: The requirement of each goals (0 < w <= 1000)
Line 3: The number of ZZY’s habits M(0 < M <= 16)
Line 4-M+4: Each line contains N integers, the ith integer represents the effect on ith goal (-1000 <= data <= 1000).
Output
For each case: The output is a single line that contains:
* the maximum number of habits ZZY can keep, followed by:
* a SORTED list (from smallest to largest) of the habit ZZY can keep. If more than one set of habits could meet the requirement, choose the set with the smallest habit numbers.
Just please output 0 if there is no way to achieve ZZY’s goals.
* the maximum number of habits ZZY can keep, followed by:
* a SORTED list (from smallest to largest) of the habit ZZY can keep. If more than one set of habits could meet the requirement, choose the set with the smallest habit numbers.
Just please output 0 if there is no way to achieve ZZY’s goals.
Sample Input
4100 200 300 4003100 100 400 500100 -10 50 300100 100 -50 -50
Sample Output
2 1 3
Author
ZZY@USC
题解:总共有M个习惯,如果用0 1两种状态表明选还是不选,那么共有2^m种情况(2^16=65536),时间复杂度为O((2^m )*m*N)
AC代码:
#include<iostream>#include<cstdlib>#include<cstdio>#include<cmath>#include<cstring>#include<string>#include<cstdlib>#include<algorithm>typedef long long LL;using namespace std;int goal[30]; //表示N个目标要达到的分数值 int f[25][25];int g[30];int main(){int n,m,ans1,ans2;while(~scanf("%d",&n)){ans1=-1;for(int i=0;i<n;i++){cin>>goal[i];}scanf("%d",&m);for(int i=0;i<m;i++) for(int j=0;j<n;j++) scanf("%d",&f[i][j]); for(int i=1;i<(1<<m);i++){ //利用二进制枚举每个习惯要还是不要 memset(g,0,sizeof(g)); int num=0; for(int j=0;j<m;j++) if(i&(1<<j)){ //如果要j这个习惯 num++; for(int k=0;k<n;k++) g[k]+=f[j][k]; } int k1; for(k1=0;k1<n;k1++) //判断是否每个目标都达到了给定的分数值 if(g[k1]<goal[k1]) break; if(k1>=n){ //更新答案 if(num>ans1) ans1=num,ans2=i; else if(num==ans1&&ans2>i) ans1=num,ans2=i; } } if(ans1==-1)puts("0");else{printf("%d",ans1);for(int i=0;i<n;i++) if(ans2&(1<<i)) printf(" %d",i+1);printf("\n"); } } return 0;}
DFS:
#include <cstdio>#include <cstring>#define M 25int a[M],b[M],mp[M][M],n,m,vis[M],ans;bool judge(){for(int i=0;i<n;i++){int sum=0;for(int j=0;j<m;j++){if(vis[j])sum+=mp[j][i];}if(sum<a[i])return false;}return true;}void dfs(int pos){if(pos==m){if(judge()){int tot=0;for(int i=0;i<m;i++)if(vis[i])tot++;if(ans<tot){ans=tot;for(int i=0;i<m;i++)b[i]=vis[i];}}return;}vis[pos]=1;dfs(pos+1);vis[pos]=0;dfs(pos+1);}int main(void){while(~scanf("%d",&n)){for(int i=0;i<n;i++)scanf("%d",&a[i]);scanf("%d",&m);for(int i=0;i<m;i++)for(int j=0;j<n;j++)scanf("%d",&mp[i][j]);memset(vis,0,sizeof(vis));memset(b,0,sizeof(b));ans=0;dfs(0);printf("%d",ans);for(int i=0;i<m;i++)if(b[i])printf(" %d",i+1);puts("");}return 0;}
1 0
- HDU 4152 ZZYs Dilemma(枚举 or dfs)
- hdu 4152 ZZY’s Dilemma【DFS】
- HDU Untitled(状压DP OR dfs枚举子集)
- hdu 4152 dfs 枚举
- hdu 4152 ZZY’s Dilemma
- hdu 4152 ZZY’s Dilemma
- hdu 4431 Mahjong (模拟,枚举+dfs)
- hdu 4770(枚举 + dfs爆搜)
- HDU 5301 Friends(DFS + 枚举)
- hdu 2208 DFS枚举
- hdu 6165(dfs or bfs or tarjan+topsort)
- HDU4152 ZZY’s Dilemma【枚举】
- hdu 2489 (dfs枚举 + prim)
- hdu 5339 Untitled(枚举 || DFS)
- hdu 1074 dfs or bfs
- hdu 1241 bfs or dfs
- 杭电 HDU ACM 1015 Safecracker (dfs 枚举排列)
- 暴力枚举,dfs(三角形篱笆,HDU 4277)
- linux C语言获取本机ip地址
- POJ 1163 The Triangle
- C++编程:XAudio2 API应用示例
- 七周七种前端框架四:Vue.js 概览
- Mysql 数据库升级shell脚本编写与测试心得
- HDU 4152 ZZYs Dilemma(枚举 or dfs)
- compose,takeUntil ,Share
- HTML5 Audio/Video 标签,属性,方法,事件汇总 (转)
- 进程与线程的区别
- epoll 水平触发和边缘触发的区别
- 最小生成树prim算法
- myeclipse打开文件选择程序
- Android 控件 下拉刷新 android-uitra-pull-to-refresh
- 【PPT制作工具】超实用!有哪些鲜为人知却好用到爆的PPT辅助工具?