zoukankan      html  css  js  c++  java
  • CodeForcesGym 100676G Training Camp

    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

    47

    解题:状压动规

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 19;
     4 int n,m,w[maxn],son[maxn],fa[maxn];
     5 vector<int>g[maxn];
     6 vector<int>o[maxn];
     7 unordered_map<string,int>ump;
     8 void read(char *str,int pos){
     9     int len = strlen(str)-1,ret = 0;
    10     while(len >= 0 && isdigit(str[len])) --len;
    11     char topic[200];
    12     strncpy(topic,str,len);
    13     topic[len] = '';
    14     for(int i = len + 1; str[i]; ++i)
    15         ret = ret*10 + str[i] - '0';
    16     ump[topic] = pos;
    17     w[pos] = ret;
    18 }
    19 void read2(char *str,int &a,int &b){
    20     int len = strlen(str) - 1;
    21     while(len >= 0 && str[len] != '>') --len;
    22     char topic[200];
    23     strncpy(topic,str,len-3);
    24     topic[len-3] = '';
    25     a = ump[topic];
    26     strcpy(topic,str + len + 2);
    27     b = ump[topic];
    28 }
    29 int dfs(int u,vector<int>(&h)[maxn]){
    30     int ret = 0;
    31     for(int i = h[u].size()-1; i >= 0; --i){
    32         ret |= (1<<h[u][i]);
    33         ret |= dfs(h[u][i],h);
    34     }
    35     return ret;
    36 }
    37 int dp[1<<maxn],day[1<<maxn];
    38 int calc(int st){
    39     if(day[st] != -1) return day[st];
    40     int x = st;
    41     st = (st&0x55555555) + ((st>>1)&0x55555555);
    42     st = (st&0x33333333) + ((st>>2)&0x33333333);
    43     st = (st&0x0F0F0F0F) + ((st>>4)&0x0F0F0F0F);
    44     st = (st&0x00FF00FF) + ((st>>8)&0x00FF00FF);
    45     st = (st&0x0000FFFF) + ((st>>16)&0x0000FFFF);
    46     return day[x] = st;
    47 }
    48 int main(){
    49     int kase,a,b;
    50     char str[200];
    51     scanf("%d",&kase);
    52     while(kase--){
    53         scanf("%d%d",&n,&m);
    54         ump.clear();
    55         getchar();
    56         for(int i = 0; i < n; ++i){
    57             g[i].clear();
    58             o[i].clear();
    59             gets(str);
    60             read(str,i);
    61             fa[i] = son[i] = 0;
    62         }
    63         for(int i = 0; i < m; ++i){
    64             gets(str);
    65             read2(str,a,b);
    66             g[a].push_back(b);
    67             o[b].push_back(a);
    68         }
    69         memset(dp,0,sizeof dp);
    70         memset(day,-1,sizeof day);
    71         for(int i = 0; i < n; ++i){
    72             son[i] = dfs(i,g);
    73             fa[i] = dfs(i,o);
    74         }
    75         m = (1<<n);
    76         for(int i = 0; i < m; ++i){
    77             for(int j = 0; j < n; ++j){
    78                 if(((i>>j)&1)) continue;
    79                 if(!(son[j]&i) && (fa[j]&i) == fa[j])
    80                     dp[i|(1<<j)] = max(dp[i|(1<<j)],dp[i] + (calc(i) + 1)*w[j]);
    81             }
    82         }
    83         printf("%d
    ",dp[m-1]);
    84     }
    85     return 0;
    86 }
    View Code
  • 相关阅读:
    cpanel 定时运行sh/php
    zencart 后台目录产品黄色icon_yellow_on.gif 解决方案
    字符串编码---hash函数的应用
    Tiling_easy version(填2 x N的格子的种类)
    n行m列的网格中含有的矩形数
    Openstack
    Docker
    Tornado模块
    LAMP应用部署
    数据库管理
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4750958.html
Copyright © 2011-2022 走看看