G. Training Camp

Time Limit: 1000ms
Memory Limit: 262144KB

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的更多相关文章

  1. Gym - 100676G Training Camp (状压dp)

    G. Training Camp[ Color: Yellow ]Montaser is planning to train very hard for ACM JCPC 2015; he has p ...

  2. 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 据 ...

  3. 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个镜子 ...

  4. Petrozavodsk Summer Training Camp 2017 Day 9

    Petrozavodsk Summer Training Camp 2017 Day 9 Problem A. Building 题目描述:给出一棵树,在树上取出一条简单路径,使得该路径的最长上升子序 ...

  5. Petrozavodsk Summer Training Camp 2017

    Petrozavodsk Summer Training Camp 2017 Problem A. Connectivity 题目描述:有\(n\)个点,现不断地加边.每条边有一种颜色,如果一个点对\ ...

  6. Petrozavodsk Winter Training Camp 2018

    Petrozavodsk Winter Training Camp 2018 Problem A. Mines 题目描述:有\(n\)个炸弹放在\(x\)轴上,第\(i\)个位置为\(p_i\),爆炸 ...

  7. 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 ...

  8. 2017 湖南省赛 K Football Training Camp

    2017 湖南省赛 K Football Training Camp 题意: 在一次足球联合训练中一共有\(n\)支队伍相互进行了若干场比赛. 对于每场比赛,赢了的队伍得3分,输了的队伍不得分,如果为 ...

  9. 2016 Al-Baath University Training Camp Contest-1

    2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...

随机推荐

  1. Lua 与C/C++ 交互系列:注冊枚举enum到Lua Code中

    在Lua Code中注冊C/C++的枚举很easy,就像注冊全局变量一样.我们使用枚举名称作为命名空间,来避免注冊的枚举发生冲突.注冊的枚举存储在全局环境(线程环境)中. 当在Lua Code中訪问枚 ...

  2. logo切图大小相应的尺寸

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTQwNDYxMQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  3. php 在同一个表单中加入和改动

    大家写站点的时候可能都会遇到这样的情况,就是写一个表单,这个表单是用来加入一篇文章的,我们屁颠屁颠的在后台接收数据,然后存入数据库.如今有个问题.当你要对该文章进行改动的时候,你是怎么处理的? 我的方 ...

  4. 初探boost之noncopyable学习笔记

    noncopyable 功能 同意程序轻松实现一个不可复制的类. 需包括头文件 #include<boost/noncopyable.hpp>     或 #include<boos ...

  5. WebP 文件及其编码解码工具(WebPconv)

    1. webp 文件 与JPEG相同,WebP 是一种有损压缩利用预测编码技术. WebP 是 Google 新推出的影像技术,它可让网页图档有效进行压缩,同时在质量相同的情况下,WebP 格式图像的 ...

  6. springboot @WebFilter过滤器的使用

    过滤器的用法就不多说了 新建Filter的继承类:MemberFilter(放置包需要注意) @WebFilter(urlPatterns = "/*") @Order(1) pu ...

  7. RocketMQ之消息中间件需要解决的问题

    消息中间件需要解决哪些问题 1.Publish/Subscribe(发布订阅) 发布订阅是消息中间件最基本的功能 2.Message Priority(消息优先级) 在消息队列中,每条消息都有不同的优 ...

  8. python3遍历选中文件夹下的文件【GUI编程】

    功能介绍 如标题 使用截图 1.第一步:运行python程序 2.第二步:点击按钮选中文件夹 3.运行结束: 1. 显示选中文件夹路径 2. 遍历打印文件夹内文件 代码 import os #程序功能 ...

  9. hdu 3729 最大匹配

    此题是我AC的HDU的201道题目.泪流满面啊! 字典序最大(最小)真是个烦人的东西. 学生i与其对应的分数区间的每个点连一条边.字典序最大,编号最大的学生开始匹配. HK无法AC啊,试了很久.我不会 ...

  10. axis2 1.7.1使用教程

    写在前面 本文只说Axis2的用法. 1.下载与部署 需要下载两个文件: 下载地址:http://mirrors.cnnic.cn/apache/axis/axis2/java/core/1.7.1/ ...