CF788C The Great Mixing BFS+思维

Posted guangheli

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF788C The Great Mixing BFS+思维相关的知识,希望对你有一定的参考价值。

这个模型十分巧妙啊,好题好题~

code: 

#include <bits/stdc++.h>
#define N 3006 
#define setIO(s) freopen(s".in","r",stdin) 
using namespace std; 
int n,k,vis[N],in[N],dp[N];
vector<int>v;       
queue<int>q;  
int main() 
 
    int i,j; 
    // setIO("input"); 
    scanf("%d%d",&n,&k); 
    for(i=1;i<=k;++i) 
    
        int x; 
        scanf("%d",&x), in[x]=1; 
    
    memset(dp,0x3f,sizeof(dp));      
    for(i=0;i<=1000;++i) 
    
        if(in[i]) 
        
            v.push_back(n-i);   
            int x=n-i+1000;   
            dp[x]=1;     
            if(x==1000) 
            
               printf("1\n"); 
               return 0; 
              
            q.push(x);      
        
    
    for(;!q.empty();) 
    
        int u=q.front();q.pop();            
        for(i=0;i<v.size();++i) 
        
            int x=u+v[i];          
            if(x>=0&&x<=2000&&dp[x]>dp[u]+1) 
            
                dp[x]=dp[u]+1;     
                q.push(x);    
            
        
     
    if(dp[1000]>=100000) printf("-1\n"); 
    else printf("%d\n",dp[1000]); 
    return 0; 

  

 

以上是关于CF788C The Great Mixing BFS+思维的主要内容,如果未能解决你的问题,请参考以下文章

The Great Mixing

Codeforces 789e The Great Mixing (bitset dp 数学)

codeforces #407(div1c div 2e)The Great Mixing

CF1221D Make The Fence Great Again

Gym101630G The Great Wall

Codeforces 1221D - Make The Fence Great Again(DP)