状压DP Gym
来源:互联网 发布:手机在linux查arp 编辑:程序博客网 时间:2024/06/05 01:09
这可能是个题目链接
题目大意:
给你n个科目,m个关系,例如A->B,表示要学习B科目,一定要把A科目学习掉。同理,如果还有C->B,那么,B就要同时学掉A和C才能学B科目。
如果你是第k天学习这个科目,那么你的val += k * W[i],这个i表示你当天学的科目。
问,怎么学习让自己的val最大。
#include <bits/stdc++.h>//#include <ext/pb_ds/tree_policy.hpp>//#include <ext/pb_ds/assoc_container.hpp>//using namespace __gnu_pbds;using namespace std;#define pi acos(-1)#define endl '\n'#define me(x) memset(x,0,sizeof(x));#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)#define close() ios::sync_with_stdio(0);typedef long long LL;const int INF=0x3f3f3f3f;const LL LINF=0x3f3f3f3f3f3f3f3fLL;const int dx[]={-1,0,1,0,-1,-1,1,1};const int dy[]={0,1,0,-1,1,-1,1,-1};const int maxn=1e3+5;const int maxx=1e6+100;const double EPS=1e-7;const int MOD=1000000007;#define mod(x) ((x)%MOD);template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}//typedef tree<pt,null_type,less< pt >,rb_tree_tag,tree_order_statistics_node_update> rbtree;/*lch[root] = build(L1,p-1,L2+1,L2+cnt); rch[root] = build(p+1,R1,L2+cnt+1,R2);中前*//*lch[root] = build(L1,p-1,L2,L2+cnt-1); rch[root] = build(p+1,R1,L2+cnt,R2-1);中后*/long long gcd(long long a , long long b){if(b==0) return a;a%=b;return gcd(b,a);}int w[maxn],dp[1<<20];map<string,int >mp;int pre[25];int main(){ int T; cin>>T; while(T--) { int n,m; mp.clear(); cin>>n>>m; cin.get(); for(int k=0;k<n;k++) { string s,name; int xx=0; getline(cin,s); int flag=1; for(int i=0;i<s.size();i++) { if(flag&&s[i+1]>='0'&&s[i+1]<='9') { flag=0; continue; } if(flag) name+=s[i]; if(!flag) xx=xx*10+s[i]-'0'; } mp[name]=k; w[k]=xx; }//用map处理数据 me(pre); for(int i=1;i<=m;i++) { string s,xx,hh; getline(cin,s); int flag=0; for(int j=0;j<s.size();j++) { if(s[j+1]=='-') { flag=1; continue; } if(flag==1&&s[j]==' ') { flag=2; continue; } if(flag==0) xx+=s[j]; if(flag==2) hh+=s[j]; } pre[mp[hh]]|=(1<<mp[xx]);//前提条件 4--100 表示先取第三天的 //5--101 表示取第一天和第三天的 } //fill(dp,dp+(1<<n),-INF); for(int i=1;i<(1<<n);i++) dp[i]=-INF; dp[0]=0; bool can[1<<19]={0}; can[0]=1; for(int st=0;st<(1<<n);st++) { if(!can[st]) continue;//如果达不到前提要求就没必要继续了 int num=0;//num表示第几天了 for(int j=0;j<n;j++) { num+=((st>>j)&1); } for(int j=0;j<n;j++) { int xx=st|(1<<j); if(xx!=st) { cout<<"xx: "<<xx<<endl; if((st&pre[j])!=pre[j])//前提条件是否满足 continue; can[xx]=1;//can[i],表示学习第i个科目之前,所需要的学习的集合是啥 dp[xx]=max(dp[xx],dp[st]+(num+1)*w[j]); } } } cout<<dp[(1<<n)-1]<<endl; }}/*13 2Implementation 3Dynamic Programming 10Greedy 7Greedy --> Dynamic ProgrammingImplementation --> Dynamic Programming*/
0 0
- 状压DP Gym
- Gym 100917EExtreme Permutations(状压dp)
- Codeforces gym 101343 J 状压dp
- Gym 101490K dp
- Gym 101201B dp
- Gym 101617D dp
- Gym 101617J dp
- Codeforces Gym 100676G Training Camp 状压dp
- Gym 100015 F Fighting for Triangles 博弈,状压dp
- GYM 100883 A.Random Fightings (状压DP)
- Gym 100499E (树DP)
- Codeforces gym 101350A dp
- Codeforces gym 101102 A dp
- 【Codeforces Gym 100228 - I】Graph Dp
- gym 100496 House of Representatives(树形dp)
- Gym 100917J Judgement(dp+bitset)
- Gym 100812I Dragon Delivers(区间dp)
- Gym 100712D Alternating Strings (dp)
- 设计模式原则—迪米特原则
- Swift 3.0 学习笔记-3-集合类型
- 云掌通使用过程中的一个小问题
- mongodb,mongovue与pycharm之大总结!!!
- Spring @Value 用法小结,#与$的区别
- 状压DP Gym
- 第七天2017/04/14(引用与const、C++基本知识)
- NC编译器结题以及软件重用性的思索
- SpringMVC原理图
- 图像分割(二)——阈值分割/区域生长/区域分裂合并法
- C# 数组最大值与最小值
- (转)ImageView 设置图片
- Qt 帧动画 (QWidget)
- 优先队列(3道优先队列问题)