2018icpc南京网络赛-E AC Challenge(状压+dfs)

题意:

n道题,每道题有ai和bi,完成这道题需要先完成若干道题,完成这道题可以得到分数t*ai+bi,其中t是时间

1s, n<=20

思路:

由n的范围状压,状态最多1e6

然后dfs,注意代码中dfs里的剪枝,

对一个状态statu,因为贪心的取最大值就行,所以及时剪枝

代码:

当时写不出来真是菜的活该

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional>

#define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x)) 

using namespace std;

typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL;

const db eps = 1e-6;
const int mod = 1e9+7;
const int maxn = 2e6+100;
const int maxm = 2e6+100;
const int inf = 0x3f3f3f3f;
const ll Inf = 0x3f3f3f3f3f3f3f3f;
const db pi = acos(-1.0);
int n;
ll dp[maxn];//达到status的最大值
ll a[30], b[30];
int s[30];
vector<int>nd[30];
void init(){
    for(int i = 1; i <= n; i++){
        int ans = 0;
        int sz = nd[i].size();
        for(int j = 0; j < sz; j++){
            ans|=(1<<(nd[i][j]-1));
        }
        s[i]=ans;
    }
    return;
}
void dfs(int cnt, int statu, ll ans){
    dp[statu] = max(dp[statu], ans);
    if(cnt == n)return;
    for(int i = 1; i <= n; i++){
        if(((1<<(i-1))&statu)!=0)continue;
        if((s[i]&statu)!=s[i])continue;
        dfs(cnt+1,statu|(1<<(i-1)), ans + (cnt+1)*a[i]+b[i]);
    }
    return;
}
int main(){
    mem(dp,-1);
    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
        int num;
        scanf("%lld %lld %d", &a[i], &b[i], &num);
        for(int j = 0; j < num; j++){
            int x;
            scanf("%d",&x);
            nd[i].pb(x);
        }
    }
    init();
    dfs(0,0,0);
    printf("%lld",dp[(1<<n)-1]<0?0:dp[(1<<n)-1]);
    return 0;
}

/*

 */

原文地址:https://www.cnblogs.com/wrjlinkkkkkk/p/10182537.html

时间: 2024-08-29 18:31:01

2018icpc南京网络赛-E AC Challenge(状压+dfs)的相关文章

2018ICPC南京网络赛

2018ICPC南京网络赛 A. An Olympian Math Problem 题目描述:求\(\sum_{i=1}^{n} i\times i! \%n\) solution \[(n-1) \times (n-1)! \% n= (n-2)!(n^2-2n+1) \%n =(n-2)!\] \[(n-2+1)\times (n-2)! \% n= (n-3)!(n^2-3n+2) \%n =(n-3)! \times 2\] 以此类推,最终只剩下\(n-1\) 时间复杂度:\(O(1)\

HDU-5025 2014广州网络赛 Saving Tang Monk 状压+BFS

给出一个N*N的矩阵,开启牢门需要收集齐m种钥匙,且必须收集了前i-1种钥匙才能收集第i种钥匙,最终收集齐了回到关押唐僧的房间拯救唐僧,经过一个'S'的房间时需要额外耗时把蛇打死,蛇最多5条,所以状压一下用优先队列BFS求最小时间即可. #include <iostream> #include <cstdio> #include <cmath> #include <queue> #include <vector> #include <cst

2018icpc南京网络赛-L Magical Girl Haze (分层图最短路)

题意: 有向图,可以把k条路的长度变为0,求1到n的最短路 思路: 将图复制k份,一共k+1层图,对于每一条i→j,都连一条低层的i→高层的j,并且权值为0 即对每一对<i,j,w>,都加边<i,j,w>,<i+n,j+n,w>,<i+2n,j+2n,w>,....,<i+kn,j+kn,w> 同时加“楼梯”<i,j+n,0>,<i+n,j+2n,0>,...,<i+(k-1)n, j+kn> 然后跑一个1~(

计蒜客- AC Challenge 状压dp

题目链接:https://nanti.jisuanke.com/t/30994 Dlsj is competing in a contest with n(0<n≤20)n (0 < n \le 20)n(0<n≤20) problems. And he knows the answer of all of these problems. However, he can submit iii-th problem if and only if he has submitted (and

2019ICPC南京网络赛A题 The beautiful values of the palace(三维偏序)

2019ICPC南京网络赛A题 The beautiful values of the palace https://nanti.jisuanke.com/t/41298 Here is a square matrix of n * nn?n, each lattice has its value (nn must be odd), and the center value is n * nn?n. Its spiral decline along the center of the squar

poj 1699 Best Sequence(AC自动机+状压DP)

题目链接:poj 1699 Best Sequence 题目大意:给定N个DNA序列,问说最少多长的字符串包含所有序列. 解题思路:AC自动机+状压DP,先对字符串构造AC自动机,然后在dp[s][i]表示匹配了s,移动到节点i时候的最短步数. #include <cstdio> #include <cstring> #include <queue> #include <vector> #include <iostream> #include &

hdu 2825 aC自动机+状压dp

Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5640    Accepted Submission(s): 1785 Problem Description Liyuan lives in a old apartment. One day, he suddenly found that there

hdu 3247 AC自动+状压dp+bfs处理

Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others)Total Submission(s): 2382    Accepted Submission(s): 750 Problem Description Great! Your new software is almost finished! The only thing left to

【HDU3341】 Lost&#39;s revenge (AC自动机+状压DP)

Lost's revenge Time Limit: 5000MS Memory Limit: 65535KB 64bit IO Format: %I64d & %I64u Description Lost and AekdyCoin are friends. They always play "number game"(A boring game based on number theory) together. We all know that AekdyCoin is t