CodeForcesGym 100676G Training Camp
2015-08-22 20:03
423 查看
G. Training Camp
Time Limit: 1000msMemory 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
47
解题:状压动规
#include <bits/stdc++.h> using namespace std; const int maxn = 19; 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)-1,ret = 0; while(len >= 0 && isdigit(str[len])) --len; char topic[200]; strncpy(topic,str,len); topic[len] = '\0'; for(int i = len + 1; str[i]; ++i) ret = ret*10 + str[i] - '0'; ump[topic] = pos; w[pos] = ret; } void read2(char *str,int &a,int &b){ int len = strlen(str) - 1; while(len >= 0 && str[len] != '>') --len; char topic[200]; strncpy(topic,str,len-3); topic[len-3] = '\0'; a = ump[topic]; strcpy(topic,str + len + 2); b = ump[topic]; } int dfs(int u,vector<int>(&h)[maxn]){ int ret = 0; for(int i = h[u].size()-1; i >= 0; --i){ ret |= (1<<h[u][i]); ret |= dfs(h[u][i],h); } return ret; } int dp[1<<maxn],day[1<<maxn]; int calc(int st){ if(day[st] != -1) return day[st]; int x = st; st = (st&0x55555555) + ((st>>1)&0x55555555); st = (st&0x33333333) + ((st>>2)&0x33333333); st = (st&0x0F0F0F0F) + ((st>>4)&0x0F0F0F0F); st = (st&0x00FF00FF) + ((st>>8)&0x00FF00FF); st = (st&0x0000FFFF) + ((st>>16)&0x0000FFFF); return day[x] = st; } int main(){ int kase,a,b; char str[200]; scanf("%d",&kase); while(kase--){ scanf("%d%d",&n,&m); ump.clear(); getchar(); for(int i = 0; i < n; ++i){ g[i].clear(); o[i].clear(); gets(str); read(str,i); fa[i] = son[i] = 0; } for(int i = 0; i < m; ++i){ gets(str); read2(str,a,b); g[a].push_back(b); o[b].push_back(a); } memset(dp,0,sizeof dp); memset(day,-1,sizeof day); for(int i = 0; i < n; ++i){ son[i] = dfs(i,g); fa[i] = dfs(i,o); } m = (1<<n); for(int i = 0; i < m; ++i){ for(int j = 0; j < n; ++j){ if(((i>>j)&1)) continue; if(!(son[j]&i) && (fa[j]&i) == fa[j]) dp[i|(1<<j)] = max(dp[i|(1<<j)],dp[i] + (calc(i) + 1)*w[j]); } } printf("%d\n",dp[m-1]); } return 0; }
View Code
相关文章推荐
- HN OJ 13375 Flowery Trails (spfa的路径遍历)
- Aizu 2302 On or Off
- poj1363——Rails
- 转:CDC,CPaintDC,CClientDC,CWindowDC区别
- (转载)BeginPaint和GetDC有什么区别?
- HDU2141Can you find it? (二分计算计算Ai+Bj+Ck = X)
- HDU 5344(MZL's xor-(ai+aj)的异或和)
- BFS(最短路) HDOJ 4308 Saving Princess claire_
- hdu 3047 Zjnu Stadium(加权并查集)2009 Multi-University Training Contest 14
- 百度地图Baidu LBS 的使用
- POJ 2248 & ZOJ 1937 Addition Chains
- Injection of resource dependencies failed解决办法总结
- int main(int argc,char* argv[])详解
- CaptainGan------ 使用“递归方法”对整个目录进行删除操作
- CaptainGan------使用递归方法实现,向FTP服务器上传整个目录结构、从FTP服务器下载整个目录到本地的功能
- NSLayoutConstraint 布局,配合简单的动画效果
- Robot Framework通过Python SMTP进行email收发测试。
- poj 1995 Raising Modulo Numbers【快速幂】
- 大整数相乘(aijud)
- Raising Modulo Numbers 1995 (快速幂+大数)