CF #321 (Div. 2) D
不说了,爆内存好几次,后来醒起状态有重复。。。
状压+TSP
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define LL long long
using namespace std;
//#pragma comment(linker, "/STACK:102400000,102400000")
const int MAX=100005; int pre[20][20];
int dish[20]; LL dp[20][1<<20]; template<class T>
inline T IN(T &num){
num = 0;
char c = getchar(), f = 0;
while(c != '-' && (c < '0' || c > '9')) c = getchar();
if(c == '-') f = 1, c = getchar();
while('0' <= c && c <= '9') num = num * 10 + c - '0', c = getchar();
if(f) num = -num;
return num;
} struct Status{
int l,s;
Status(){};
Status(int ll,int ss){l=ll,s=ss;}
}; int main(){
int n,m,k;
int x,y,c; while(scanf("%d%d%d",&n,&m,&k)!=EOF){
queue<Status>que;
memset(pre,0,sizeof(pre));
memset(dp,-1,sizeof(dp));
for(int i=1;i<=n;i++)
IN(dish[i]);
for(int i=1;i<=k;i++){
IN(x),IN(y),IN(c);
pre[x][y]=c;
}
LL ans=0;
for(int i=1;i<=n;i++){
dp[i][1<<i]=dish[i];
ans=max(ans,(LL)dish[i]);
que.push(Status(i,1<<i));
}
Status tmp; int e;
for(int i=2;i<m;i++){
int sz=que.size();
while(sz--){
tmp=que.front();
que.pop();
for(int j=1;j<=n;j++){
if(tmp.s&(1<<j)){
}
else{
k=tmp.l; e=tmp.s|(1<<j);
if(dp[j][e]==-1) que.push(Status(j,e));
dp[j][e]=max(dp[j][e],dp[k][tmp.s]+pre[k][j]+dish[j]);
}
}
}
}
if(m>=2){
while(!que.empty()){
tmp=que.front();
que.pop();
for(int j=1;j<=n;j++){
if(tmp.s&(1<<j)){
}
else{
k=tmp.l; e=tmp.s|(1<<j);
dp[j][e]=max(dp[j][e],dp[k][tmp.s]+pre[k][j]+dish[j]);
ans=max(ans,dp[j][e]);
}
}
}
}
cout<<ans<<endl; }
return 0;
}
CF #321 (Div. 2) D的更多相关文章
- CF #321 (Div. 2) E
用线段树维护哈希,类似于进位制的一个哈希 a[i]*p^i+a[i-1]*p^i-1... 然后,线段树存在某区间的哈希的值,对于更新,则只需提前计算出整段的哈希值即可. 判断是否相等,由于相隔为d, ...
- CF #376 (Div. 2) C. dfs
1.CF #376 (Div. 2) C. Socks dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...
- CF #375 (Div. 2) D. bfs
1.CF #375 (Div. 2) D. Lakes in Berland 2.总结:麻烦的bfs,但其实很水.. 3.题意:n*m的陆地与水泽,水泽在边界表示连通海洋.最后要剩k个湖,总要填掉多 ...
- CF #374 (Div. 2) D. 贪心,优先队列或set
1.CF #374 (Div. 2) D. Maxim and Array 2.总结:按绝对值最小贪心下去即可 3.题意:对n个数进行+x或-x的k次操作,要使操作之后的n个数乘积最小. (1)优 ...
- CF #374 (Div. 2) C. Journey dp
1.CF #374 (Div. 2) C. Journey 2.总结:好题,这一道题,WA,MLE,TLE,RE,各种姿势都来了一遍.. 3.题意:有向无环图,找出第1个点到第n个点的一条路径 ...
- CF #371 (Div. 2) C、map标记
1.CF #371 (Div. 2) C. Sonya and Queries map应用,也可用trie 2.总结:一开始直接用数组遍历,果断T了一发 题意:t个数,奇变1,偶变0,然后与问的 ...
- CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组
题目链接:CF #365 (Div. 2) D - Mishka and Interesting sum 题意:给出n个数和m个询问,(1 ≤ n, m ≤ 1 000 000) ,问在每个区间里所有 ...
- CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组(转)
转载自:http://www.cnblogs.com/icode-girl/p/5744409.html 题目链接:CF #365 (Div. 2) D - Mishka and Interestin ...
- CF#138 div 1 A. Bracket Sequence
[#138 div 1 A. Bracket Sequence] [原题] A. Bracket Sequence time limit per test 2 seconds memory limit ...
随机推荐
- http通讯基础
1 . 一个网页包括 JS CSS Html 2 . 状态码:200 正常 302 临时重定向 (类似呼叫转移) 304 未修改,客户端缓存的信息是最新的,无需到服务器重新获取 403 ...
- AcWing算法基础1.1
排序 快速排序(快排) 写题的时候用的不多基本都是直接sort ( ),面试可能要手撸快排,上模板 void quick_sort(int q[], int l, int r) { if(l > ...
- hbuilder中的 http://www.w3.org/TR/html4/strict.dtd
<!-- This is HTML 4.01 Strict DTD, which excludes the presentation attributes and elements that W ...
- struts2.1.6存在中文乱码的bug
如题,后续版本中已解决:可以通过添加filter的方式解决.
- JS高级——原型链
构造函数 构造函数是特殊的函数,里面没有returen返回值 new的过程中,会自动将对象返回,不需要return new的过程中,会执行函数中的代码,会将创建的对象赋值给构造函数中的this 基本概 ...
- jQuery——多库共存
多库共存:jQuery占用了$ 和jQuery这两个变量.当在同一个页面中引用了jQuery这个js库,并且引用的其他库(或者其他版本的jQuery库)中也用到了$或者jQuery这两个变量,那么,要 ...
- JS——try catch throw
本例检测输入变量的值.如果值是错误的,会抛出一个异常(错误).catch 会捕捉到这个错误,并显示一段自定义的错误消息: <script> function myFunction() { ...
- struts2.5.2 通配符问题_亲测有用
学了一段时间struts2,跟着教程做,但发现struts2的版本不同,很多东西的使用是有差异的.例如之前遇到的创建sessionFactory的方式就跟之前版本有着明显的差异.今天又遇到一个问题,那 ...
- Python 之selenium+phantomJS斗鱼抓取案例
from selenium import webdriver from bs4 import BeautifulSoup import time if __name__ == '__main__': ...
- python 简单简绍以及简单的语法
一.Pthon介绍 Pyhton的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆在阿姆斯特丹打发时间,决心开发一款新的脚本解释程序,作为ABC语言的 ...