zoj 3305 - Get Sauce
来源:互联网 发布:留学低龄化 知乎 编辑:程序博客网 时间:2024/05/22 02:03
题目:打酱油(⊙v⊙),有n种原料,和一些配方,现在要在每种原料最多用一次的情况下,配出最多的配方。
分析:dp,01背包。因为原料种类少,可以用2进制表示每种配方,按配方01背包即可,(容积(1<<n)-1)。
这里在计算过程中需要剪枝,否则会TLE,配方有包含关系取下的即可。(Orz大黄。。)
说明:本来觉得搜索状态会快一点,结果剪枝后还 1120ms。。。
#include <stdio.h>#include <stdlib.h>#include <string.h>int Sauce[ 50001 ][ 16 ];int Count[ 50001 ];int Value[ 50001 ];bool U[ 50001 ];int F[ 1<<16 ];int value( int id ){ int sum = 0; for ( int i = 0 ; i < Count[ id ] ; ++ i ) sum += (1<<(Sauce[ id ][ i ]-1)); return sum;}int cmp( const void* a, const void* b ){ return *((int *)b) - *((int *)a);}int main(){ int n,m; while ( scanf("%d%d",&m,&n) != EOF ) { for ( int i = 1 ; i <= n ; ++ i ) { scanf("%d",&Count[ i ]); for ( int j = 0 ; j < Count[ i ] ; ++ j ) scanf("%d",&Sauce[ i ][ j ]); } memset( F, 0, sizeof( F ) ); memset( U, 0, sizeof( U ) ); for ( int i = 1 ; i <= n ; ++ i ) Value[ i ] = value( i ); qsort( &Value[ 1 ], n , sizeof( int ), cmp ); for ( int i = 1 ; i <= n ; ++ i ) for ( int j = i+1 ; j <= n ; ++ j ) if ( (Value[ i ]&Value[ j ]) == Value[ j ] ) { U[ i ] = 1; break; } int M = (1<<m)-1; for ( int i = 1 ; i <= n ; ++ i ) { if ( U[ i ] ) continue; for ( int j = M ; j >= 0 ; -- j ) if ( (j&Value[ i ]) == Value[ i ] && F[ j ] <= F[ j-Value[ i ] ] ) F[ j ] = F[ j-Value[ i ] ] + 1; } printf("%d\n",F[ M ]); } return 0; }
0 0
- zoj 3305 - Get Sauce
- ZOJ 3305 Get Sauce
- ZOJ 3305 Get Sauce(DFS+剪枝)
- Get Sauce
- ZOJ3305 Get Sauce
- ZOJ3305 Get Sauce(最大流)
- Sauce Labs
- ZOJ3305Get Sauce 状压DP,
- Pasta with Quick Meat Sauce
- zoj 2711 Get Out of the Glass
- zoj 1716 Get Many Persimmon Trees
- ZOJ 1716 Get Many Persimmon Trees (暴力)
- zoj 2771 - Get Out of the Glass
- POJ 3094/ ZOJ 2812: Quicksum 【.get()】
- 云测试服务Sauce Labs介绍
- zoj 3305
- ZOJ-3305
- zoj 1716 || poj 2029 Get Many Persimmon Trees
- 如何在html静态页面中使用struts2标签
- (DS 《算法竞赛入门经典》)UVA 11997 K Smallest Sums
- JSON解析 http://johncookie.iteye.com/blog/1140186
- 详解C#委托,事件与回调函数
- WPF 模板介绍
- zoj 3305 - Get Sauce
- 如何判断数据库的可串行化调度
- Clipboard 剪切板的应用&&Action
- iOS开发--删除本地相册照片
- easyUI——datebox验证和自定义取消按钮
- ITK配准:基于手动设置地标的刚性配准
- 从捕包数据上看终端设备3种加入网络过程
- hrbust1053 Warcraft III (完全背包裸题)
- 【SpringMVC系列二】 EasyUI+SpringMVC--Maven项目--WEB-INF安全目录访问jsp