BAPC 2014 Preliminary Talent Selection (bfs)

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include  <set>
 6 #include <map>
 7 #include <vector>
 8 #include <cmath>
 9 #include <queue>
10 using namespace std;
11 typedef long long ll;
12 const int inf=0x3f3f3f3f;
13 int T;
14 int  a[50];
15 int d[8000];//数组的下标可以为负的
16 //d[i]表示 尽量凑成i需要的最小时间数目
17 struct Node{
18     int step,time;
19 }sta,ed,cnt,tp;
20 int main()
21 {
22     scanf("%d",&T);
23      while(T--)
24      {   int n,t;
25          scanf("%d%d",&n,&t);
26          for(int i=0;i<n;i++)
27          scanf("%d",&a[i]);
28          memset(d,inf,sizeof(d));
29          d[0]=0;
30          sta.step=0,sta.time=0;
31          ed.time=inf;//要求的
32          queue<Node>q;
33         while(!q.empty()){
34             q.pop();//清空队列
35         }
36          q.push(sta);
37          while(!q.empty()){
38              tp=q.front();
39              q.pop();
40              if(tp.time>=t){ //满足时间的
41                  if(ed.time>tp.time) ed=tp;//先比时间,要尽量接近t
42                  else if(ed.time==tp.time&&ed.step>tp.step) 
43                      ed=tp;
44                  //时间考虑完后,要step尽量少
45              }
46              for(int i=0;i<n;i++){
47                  int nex=tp.time+a[i];
48                  if(nex<0) nex=0;
49                 if(nex>3600) nex=3600;
50                  if(d[nex]>=inf){//最先到达nex的时间单元数目一定最少
51                      d[nex]=d[tp.time]+1;
52                      cnt.step=d[nex];
53                      cnt.time=nex;
54                      q.push(cnt);
55                  }
56              }
57          }
58          printf("%d %d\n",ed.step,ed.time-t);
59      }
60      
61      return  0;
62 }

猜你喜欢

转载自www.cnblogs.com/tingtin/p/9291634.html
今日推荐