BFS (图)——Codeforces 788 C. The Great Mixing

题目

           **The Great Mixing**

Sasha and Kolya decided to get drunk with Coke, again. This time they have k types of Coke. i-th type is characterised by its carbon dioxide concentration . Today, on the party in honour of Sergiy of Vancouver they decided to prepare a glass of Coke with carbon dioxide concentration . The drink should also be tasty, so the glass can contain only integer number of liters of each Coke type (some types can be not presented in the glass). Also, they want to minimize the total volume of Coke in the glass.

Carbon dioxide concentration is defined as the volume of carbone dioxide in the Coke divided by the total volume of Coke. When you mix two Cokes, the volume of carbon dioxide sums up, and the total volume of Coke sums up as well.

Help them, find the minimal natural number of liters needed to create a glass with carbon dioxide concentration . Assume that the friends have unlimited amount of each Coke type.

input

The first line contains two integers n, k (0 ≤ n ≤ 1000, 1 ≤ k ≤ 106) — carbon dioxide concentration the friends want and the number of Coke types.

The second line contains k integers a1, a2, …, ak (0 ≤ ai ≤ 1000) — carbon dioxide concentration of each type of Coke. Some Coke types can have same concentration.

output

Print the minimal natural number of liter needed to prepare a glass with carbon dioxide concentration , or -1 if it is impossible.

Example

>Input

>400 4
>100 300 450 500

>Output

>2

>Input

>50 2
>100 25

>Output

>3

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#define  INF  0x3f3f3f3f
#define  maxi 2000 + 10
#define  maxj 1000 + 10
using namespace std;

vector <int> v;
int n, k;
int dp[maxi];
int in[maxj];
queue <int > Q;

void bfs()
{

     for (int i = 0; i <= 1000; i++)
     {
         if (in[i])
            {
                v.push_back(n-i);
                int x = n - i + 1000;
                dp[x] = 1;
                if (x == 1000) break;
                Q.push(x);
            }
     }

     while(Q.size())
     {
         int t = Q.front(); Q.pop();

         for (int i = 0; i < v.size(); i++)
         {
             int x = t + v[i];
             if (x >= 0 && x <= 2000 && dp[x] > dp[t] + 1)
             {
                 dp[x] = dp[t] + 1;
                 Q.push(x);
             }
         }
     }


}




int main()
{
    memset(in,0,sizeof(in));
    memset(dp,INF,sizeof(dp));

   cin >> n >> k;

   int b;
   for (int i = 1; i <= k; i++)
     {
         scanf("%d",&b);
         in[b] = 1;
     }

   bfs();

   if (dp[1000] >= INF)  cout << "-1" << endl;
    else cout << dp[1000] <<endl;

   return 0;
}

解析:

1.bfs
2.图
3.状态转换
4.S(ki * (n - a[i]) =0) ki 和最小(推导)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值