Codeforces Round #321 (Div. 2) D. Kefa and Dishes 状压dp
2015-09-23 10:52
381 查看
[b]D. Kefa and Dishes[/b]
Time Limit: 1 SecMemory Limit: 256 MB
题目连接
http://codeforces.com/contest/580/problem/DDescription
When Kefa came to the restaurant and sat at a table, the waiter immediately brought him the menu. There were n dishes. Kefa knows that he needs exactly m dishes. But at that, he doesn't want to order the same dish twice to taste as many dishes as possible.Kefa knows that the i-th dish gives him ai units of satisfaction. But some dishes do not go well together and some dishes go very well together. Kefa set to himself k rules of eating food of the following type — if he eats dish x exactly before dish y (there should be no other dishes between x and y), then his satisfaction level raises by c.
Of course, our parrot wants to get some maximal possible satisfaction from going to the restaurant. Help him in this hard task!
Input
The first line of the input contains three space-separated numbers, n, m and k (1 ≤ m ≤ n ≤ 18, 0 ≤ k ≤ n * (n - 1)) — the number of dishes on the menu, the number of portions Kefa needs to eat to get full and the number of eating rules.The second line contains n space-separated numbers ai, (0 ≤ ai ≤ 109) — the satisfaction he gets from the i-th dish.
Next k lines contain the rules. The i-th rule is described by the three numbers xi, yi and ci (1 ≤ xi, yi ≤ n, 0 ≤ ci ≤ 109). That means that if you eat dish xi right before dish yi, then the Kefa's satisfaction increases by ci. It is guaranteed that there are no such pairs of indexes i and j (1 ≤ i < j ≤ k), that xi = xj and yi = yj.
Output
In the single line of the output print the maximum satisfaction that Kefa can get from going to the restaurant.Sample Input
2 2 1 1 1 2 1 1
Sample Output
3
HINT
题意一个人有n道菜,然后要点m道菜,每道菜有一个美味程度
然后给你了很多个关系,表示如果x刚好在y前面做的话,他的美味程度就会增加c
题解:
裸的状压dp,记忆化搜索也可以跑
看到这个数据范围就是状压= =
![](file:///D:/record_of_qq/475517977/Image/C2C/Y%7D)@)1%KBO0HM418$J94$1R.jpg)
代码:
//qscqesze #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <bitset> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::ssecondnc_with_stdio(0);cin.tie(0) #define maxn 20 #define mod 1000000007 #define eps 1e-9 #define PI acos(-1) const double EP = 1E-10 ; int Num; //const int inf=0first7fffffff; const ll inf=999999999; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } //************************************************************************************* ll dp[1<<18][20]; ll a[maxn],M[maxn][maxn]; int n,m,k; ll dfs(int x,int y,int z) { if( dp[x][y] !=-1 ) return dp[x][y]; dp[x][y]=0; if( z==m )return dp[x][y]; for(int i=0;i<n;i++) { if( ((x >> i) & 1) == 0) { dp[x][y]=max( dp[x][y], dfs( x | (1<<i) , i , z+1 ) + M[y][i] + a[i] ); } } return dp[x][y]; } int main() { memset(dp,-1,sizeof(dp)); n=read(),m=read(),k=read(); for(int i=0;i<n;i++) a[i]=read(); for(int i=0;i<k;i++) { int x=read(),y=read(); ll c=read(); M[x-1][y-1]+=c; } printf("%lld\n",dfs(0,n+1,0)); }
相关文章推荐
- JavaScript学习总结(九)事件详解
- 笔记08 throw e 和throw 的区别
- 面向对象的5个基本设计原则:
- solr + zookeeper 组建 solrcloud
- 一个Android事件注入框架详解
- FLT_MAX 和 FLT_MIN的定义
- 在Ubuntu上实现Sensor Explorer
- JavaScript学习总结(八)正则表达式
- web 前端 错误监控
- centos6.5下Zabbix系列之Zabbix安装搭建及汉化
- mongodb shell浮点数转为整数 NumberInt()
- PHP提高编程效率的20个要点
- 一种简单的c++文件操作方式
- 正排索引和倒排索引
- 【Code Forces 321E】【线段树+字符串哈希】Kefa and Watch 字符串修改 循环节判定
- CIR,CBS,EBS,PIR,PBS傻傻分不清楚?看这里!----揭秘令牌桶
- Makefile和Makefile.sh在同一级目录下引发的问题
- JavaScript学习总结(七)Ajax和Http状态字
- Largest Point
- CSS代码格式化工具