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 题意:比赛 ...
随机推荐
- hdu 1698 Just a Hook 基本线段树
使用线段树更新每段区间的奖(1,2,3),最后在统计整段区间的数和,基本线段树,果断1A啊 #include<iostream> #include<stdio.h> using ...
- [CSharp] C#开源大全
商业协作和项目管理平台-TeamLab 网络视频会议软件-VMukti 驰骋工作流程引擎-ccflow [免费]正则表达式测试工具-Regex-Tester Windows-Phone-7-SDK E ...
- javaWeb中URLEncoder.encode编码需要调用两次
今天碰到一个问题,在Controller类中一个方法跳转到该类中的另一个方法,带着中文参数,在跳转之前对该参数进行编码: msg = java.net.URLEncoder.encode(msg,&q ...
- html/css常用合集
1. 消除inline-block元素间的换行间隙问题: {font-size:0;} 兼容IE6/7浏览器的方法:letter-spacing属性. 2.让两个inline-block的div顶 ...
- LeetCode Weekly Contest 27
1. 557. Reverse Words in a String III 分割字符串,翻转. class Solution { public: string reverseWords(string ...
- Android网络编程随想录(3)
大多数Android的app都会使用HTTP协议来发送和接收数据.在Android开发中,通常使用两种http客户端:一个是Apache的HttpClient,另一个是HttpURLConnectio ...
- BZOJ1096: [ZJOI2007]仓库建设(dp+斜率优化)
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 5790 Solved: 2597[Submit][Status][Discuss] Descript ...
- vue-cli简介(中文翻译)
vue-cli是一个简单的vuejs脚手架命令行工具. 安装 准备:Node.js(>=4.x,推荐6.x版本),npm版本3以上和Git. $npm install -g vue-cli 使用 ...
- jQuery操作DOM知识总结
jquery操作DOM(节点) 1.创建元素 //$(htmlStr) //htmlStr:html格式的字符串 $("<span>这是一个span元素</span> ...
- (转)Bootstrap 之 Metronic 模板的学习之路 - (6)自定义和扩展
https://segmentfault.com/a/1190000006815041 前面我们将 Metronic 的结构和源码大致浏览了一遍,Metronic 整个文件包有三百多兆,在实际项目中, ...