CodeForcesGym 100676G Training Camp
G. Training Camp
This problem will be judged on CodeForcesGym. Original ID: 100676G
64-bit integer IO format: %I64d Java class name: (Any)
Montaser is planning to train very hard for ACM JCPC 2015; he has prepared a list with n topics
to study in the next n days, one topic every day. Montaser knows that some topics depend on other topics, so he asked coach Fegla and got a list
of m constraints on the order in which he should study these topics. Also, coach Fegla told him that when he studies topic x on the kth day (1 ≤ k ≤ n), his level will
increase by k*Wx, where Wx is a weight for topic x, representing how hard it is. Given the list of topics, the weight of each topic, and the list of constrains, can you tell Montaser
what is the maximum level he can reach in these n days? He is currently at level 0.
Input
The first line of input contains one integer T representing the number of test cases (1 ≤ T ≤ 128).
The first line of each test case contains two integers: n and m (1 ≤ n ≤ 18).
The next n lines, each contains the title of one of the topics followed by a space, then an integer
W that represents the weight of this topic (1 ≤ W ≤ 100).
The next m lines are of the form: Topic 1 --> Topic 2, which means that Topic 1 must be studied
before Topic 2.
Titles contain only English letters and spaces (no more than 40 characters).
Test cases are separated by a blank line.
Output
For each test case, print the maximum level that Montaser can reach.
Sample Input
1
3 2
Implementation 3
Dynamic Programming 10
Greedy 7
Greedy --> Dynamic Programming
Implementation --> Dynamic Programming
Sample Output
解题:状压动规
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
int n,m,w[maxn],son[maxn],fa[maxn];
vector<int>g[maxn];
vector<int>o[maxn];
unordered_map<string,int>ump;
void read(char *str,int pos){
int len = strlen(str)-,ret = ;
while(len >= && isdigit(str[len])) --len;
char topic[];
strncpy(topic,str,len);
topic[len] = '\0';
for(int i = len + ; str[i]; ++i)
ret = ret* + str[i] - '';
ump[topic] = pos;
w[pos] = ret;
}
void read2(char *str,int &a,int &b){
int len = strlen(str) - ;
while(len >= && str[len] != '>') --len;
char topic[];
strncpy(topic,str,len-);
topic[len-] = '\0';
a = ump[topic];
strcpy(topic,str + len + );
b = ump[topic];
}
int dfs(int u,vector<int>(&h)[maxn]){
int ret = ;
for(int i = h[u].size()-; i >= ; --i){
ret |= (<<h[u][i]);
ret |= dfs(h[u][i],h);
}
return ret;
}
int dp[<<maxn],day[<<maxn];
int calc(int st){
if(day[st] != -) return day[st];
int x = st;
st = (st&0x55555555) + ((st>>)&0x55555555);
st = (st&0x33333333) + ((st>>)&0x33333333);
st = (st&0x0F0F0F0F) + ((st>>)&0x0F0F0F0F);
st = (st&0x00FF00FF) + ((st>>)&0x00FF00FF);
st = (st&0x0000FFFF) + ((st>>)&0x0000FFFF);
return day[x] = st;
}
int main(){
int kase,a,b;
char str[];
scanf("%d",&kase);
while(kase--){
scanf("%d%d",&n,&m);
ump.clear();
getchar();
for(int i = ; i < n; ++i){
g[i].clear();
o[i].clear();
gets(str);
read(str,i);
fa[i] = son[i] = ;
}
for(int i = ; i < m; ++i){
gets(str);
read2(str,a,b);
g[a].push_back(b);
o[b].push_back(a);
}
memset(dp,,sizeof dp);
memset(day,-,sizeof day);
for(int i = ; i < n; ++i){
son[i] = dfs(i,g);
fa[i] = dfs(i,o);
}
m = (<<n);
for(int i = ; i < m; ++i){
for(int j = ; j < n; ++j){
if(((i>>j)&)) continue;
if(!(son[j]&i) && (fa[j]&i) == fa[j])
dp[i|(<<j)] = max(dp[i|(<<j)],dp[i] + (calc(i) + )*w[j]);
}
}
printf("%d\n",dp[m-]);
}
return ;
}
CodeForcesGym 100676G Training Camp的更多相关文章
- Gym - 100676G Training Camp (状压dp)
G. Training Camp[ Color: Yellow ]Montaser is planning to train very hard for ACM JCPC 2015; he has p ...
- 2015 UESTC Winter Training #7【2010-2011 Petrozavodsk Winter Training Camp, Saratov State U Contest】
2015 UESTC Winter Training #7 2010-2011 Petrozavodsk Winter Training Camp, Saratov State U Contest 据 ...
- 2015-2016 Petrozavodsk Winter Training Camp, Nizhny Novgorod SU Contest (5/9)
2015-2016 Petrozavodsk Winter Training Camp, Nizhny Novgorod SU Contest B. Forcefield 题意 给你一维平面上n个镜子 ...
- Petrozavodsk Summer Training Camp 2017 Day 9
Petrozavodsk Summer Training Camp 2017 Day 9 Problem A. Building 题目描述:给出一棵树,在树上取出一条简单路径,使得该路径的最长上升子序 ...
- Petrozavodsk Summer Training Camp 2017
Petrozavodsk Summer Training Camp 2017 Problem A. Connectivity 题目描述:有\(n\)个点,现不断地加边.每条边有一种颜色,如果一个点对\ ...
- Petrozavodsk Winter Training Camp 2018
Petrozavodsk Winter Training Camp 2018 Problem A. Mines 题目描述:有\(n\)个炸弹放在\(x\)轴上,第\(i\)个位置为\(p_i\),爆炸 ...
- Warsaw U Contest Petrozavo dsk Summer 2011 Training Camp, Monday, September 5, 2011
Warsaw U Contest Petrozavo dsk Summer 2011 Training Camp, Monday, September 5, 2011 Problem A.Chocol ...
- 2017 湖南省赛 K Football Training Camp
2017 湖南省赛 K Football Training Camp 题意: 在一次足球联合训练中一共有\(n\)支队伍相互进行了若干场比赛. 对于每场比赛,赢了的队伍得3分,输了的队伍不得分,如果为 ...
- 2016 Al-Baath University Training Camp Contest-1
2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...
随机推荐
- fastjson null 值处理
偶然用到fastjson转换json 在前台用js解析竟然某些字段没有,曾经用过gson.联想到是不是相似gson默认将null值不显示了,找了下资料果真如此 直接上代码吧 import java.u ...
- Maximal Rectangle [leetcode] 的三种思路
第一种方法是利用DP.时间复杂度是 O(m * m * n) dp(i,j):矩阵中同一行以(i,j)结尾的所有为1的最长子串长度 代码例如以下: int maximalRectangle(vecto ...
- ubuntu14.04 安装LNMP
新书上市<深入解析Android 5.0系统> 通常我们使用centos来组建LNMP,可是我们开发时多使用ubuntu的桌面版本号来调试,以下将具体介绍怎样在ubuntu上安装一套LNM ...
- getLocationInWindow getLocationOnScreen getLeft , getTop, getBottom,getRight
版权声明:本文为博主原创文章,未经博主允许不得转载. 最近做项目时,发现在activity的onCreate()和onResume()方法里调用View.getLocationInWindow() 时 ...
- Java基础学习分享
一.Java介绍 Java是由原Sun公司(现已被甲骨文公司收购)于1991年开发的编程语言,初衷是为智能家电的程序设计提供一个分布式代码系统.为了使整个系统与平台无关,采用了虚拟机器码方式,虚拟机内 ...
- Nginx实现负载均衡 + Keepalived实现Nginx的高可用
前言 使用集群是大中型网站解决高并发.海量数据问题的常用手段.当一台服务器的处理能力.存储空间不足时,不要企图去换更强大的服务器,对大型网站而言,不管多么强大的服务器,都满足不了网站持续增长的业务需求 ...
- 09-JavaScript高级
今日知识 1. Dom(文档对象模型) 2. Bom(浏览器对象模型) 3. 总结 Dom 1. 获取id为div1的元素对象. * var result=document.getElementByI ...
- BZOJ 4742 DP
思路: Claris大大说了 排序以后 这个可以看成是括号序列 f[i][j][k]表示到了i j个左括号 k个右括号 (f[i][j][k]+=f[i-1][j][k])%=p; if(node[i ...
- Spark Scala语言学习系列之完成HelloWorld程序(三种方式)
三种方式完成HelloWorld程序 分别采用在REPL,命令行(scala脚本)和Eclipse下运行hello world. 一.Scala REPL. windows下安装好scala后,直接C ...
- junit使用第一弹
知识点——断言 断言是编写测试用例的核心实现方式,即期望值是多少,测试的结果是多少,以此来判断测试是否通过. 1. 断言核心方法 assertArrayEquals(expecteds, actual ...