ZOJ 1639 Hang Up the System
来源:互联网 发布:流程图软件 性价比高 编辑:程序博客网 时间:2024/05/16 02:06
题目链接~~>
做题感悟:这题其实用深搜+状态压缩也可以,说不定更快。
解题思路:
把不可以同时运行,的位于一组中的压缩成二进制这样标记一下(表示不可以计算价值),然后枚举每一种状态如果不合法就标记一下,最后在枚举一下每一种状态,记录合法状态的最优解。
代码:
#include<stdio.h>#include<map>#include<queue>#include<cmath>#include<string.h>#include<iostream>#include<iomanip>#include<algorithm>#include<stdlib.h>#define INT unsigned long long intusing namespace std ;const int MY = 100 + 10 ;const int MX = 2000 + 10 ;int n,mx ;int w[MX] ;bool vis[1<<16] ;map<string,int>m ;void solve(char *s1,int num)// 分解字符串{ string sx="" ; int key=0 ; int len=strlen(s1),st=0,ed ; for(int i=0 ;i<len ;i++) { if(s1[i]==' '||i==len-1) { sx="" ; if(i==len-1) ed=len ; else ed=i ; for(int j=st ;j<ed ;j++) sx+=s1[j] ; key=key|(1<<m[sx]) ; st=i+1 ; } } vis[key]=true ;}void DP(){ int best=0 ; for(int S=0 ;S<(1<<n) ;S++) // 枚举每一种状态标记不合法状态 for(int i=0 ;i<n ;i++) if((S&(1<<i))&&vis[S^(1<<i)]) { vis[S]=true ; break ; } for(int S=0 ;S<(1<<n) ;S++) // 取合法状态记录最优解 { if(vis[S]) continue ; int ans=0 ; for(int i=0 ;i<n ;i++) if(S&(1<<i)) ans+=w[i] ; best = max(ans,best) ; } cout<<best<<endl ;}void input(){ string s="" ; char s1[MX] ; m.clear() ; memset(vis,false,sizeof(vis)) ; for(int i=0 ;i<n ;i++) { cin>>s>>w[i] ; m[s]=i ; } scanf("%d",&mx) ; getchar() ; for(int i=0 ;i<mx ;i++) { gets(s1) ; solve(s1,i) ; }}int main(){ int cse=1 ; while(cin>>n,n) { input() ; cout<<"System "<<cse++<<": " ; DP() ; } return 0 ;}
0 0
- ZOJ 1639 Hang Up the System
- ZOJ 1639 Hang Up the System (状态压缩)
- Error: socket hang up
- zoj 1050 Start Up the Startup
- ZOJ 1050 start up the startup
- Dig Up System Information Using the Terminal
- FATAL: the database system is starting up
- Hang up voice calls(转)
- ZOJ 2966 Build The Electric System (kruskal)
- ZOJ 2966 Build The Electric System MST
- ZOJ 1965 Build The Electric System
- ZOJ 2966 Build The Electric System
- zoj 2966 Build The Electric System
- ZOJ 2966Build The Electric System
- iOS Simulator hang up ( Xcode4.6.3)
- How do I look up for the system locale
- Cleaning up the system – 11i and R12
- postgresql主备之the database system is starting up
- 8.2_java学习_装饰设计模式
- Poj 3928 Ping pong 树状数组
- @selector
- 线程与进程
- I NEED A OFFER 01背包
- ZOJ 1639 Hang Up the System
- 51Talk-Level 7 Unit 1 L1
- 线段树(单点更新,区间求和)
- 数据库连接问题——服务器启动不了
- CF 453A . Little Pony and Crystal Mine
- HttpClient简单用法
- 解剖 MongoDB 【1】系统概览
- android 获取 内外存储设备路径疑问
- VMware网络模式介绍