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 题意:比赛 ...
随机推荐
- STM32F4——GPIO基本应用及复用
IO基本应用 一.IO基本结构: 针对STM32F407有7组IO.分别为GPIOA~GPIOG,每组IO有16个IO口,则有112个IO口. 当中IO口的基本结构例如以下: 二.工作方式: ST ...
- android高速上手(二)android开发环境搭建及hello world
基本了解了java语法,下一步.我们一起开启hello world的神奇之旅. (一)android开发环境搭建 之前搭建android开发环境是件很费力的事情,下载Eclipse.安装ADT等,现在 ...
- POJ3185 The Water Bowls 反转(开关)
Description The cows have a line of 20 water bowls from which they drink. The bowls can be either ri ...
- [转]详细解读TrueSkill 排名系统
概要 大多数竞技游戏都有一个评价玩家是否完成目标的度量指标,它是游戏的基础.对于包含两个或两个以上玩家(多玩家比赛)的比赛,常涉及到游戏玩家技能的排名方法.游戏鼓励玩家之间相互竞争,玩家不只要赢得单场 ...
- [POJ 3621] Sightseeing Cows
[题目链接] http://poj.org/problem?id=3621 [算法] 01分数规划(最优比率环) [代码] #include <algorithm> #include &l ...
- struts的工作流程
- 一个请求过来,走前端控制器StrutsPrepareAndExecuteFilter -前端控制器是一个过滤器,过滤器中的核心方法是doFilter(),doFilter方法中首先处 ...
- Could not open ServletContext resource [/WEB-INF/Dispatcher-servlet.xml]
转自:https://blog.csdn.net/mafan121/article/details/44833201 配置spring时出现了如下错误: 默认的DispatcherServlet在初始 ...
- yii依赖注入
为了降低代码耦合程度,提高项目的可维护性,Yii采用多许多当下最流行又相对成熟的设计模式,包括了依赖注入(Denpdency Injection, DI)和服务定位器(Service Locator) ...
- C++ 类型转换操作与操作符重载 operator type() 与 type operator()
类型转换操作符(type conversion operator)是一种特殊的类成员函数,它定义将类类型值转变为其他类型值的转换.转换操作符在类定义体内声明,在保留字 operator 之后跟着转换的 ...
- Mybatis xml约束文件的使用
一:准备.DTD约束文件 核心配置文件约束文件:mybatis-config.dtd <?xml version="1.0" encoding="UTF- ...