用优先队列BFS一遍即可,
每个节点分别记录 当前难度,加上下一个以后的难度,和下一个为哪道题
队列优先弹出加上下一个以后难度最小的
#include "stdio.h" #include "string.h" #include "algorithm" #include "queue" using namespace std; struct node { int now,next,id; bool friend operator<(node n1,node n2) { return n2.next<n1.next; } }; int a[10010],n,m; void bfs() { int i,cnt; priority_queue<node>q; node cur,next; scanf("%d%d",&n,&m); for (i=0;i<n;i++) scanf("%d",&a[i]); sort(a,a+n); cur.now=0; cur.next=a[0]; cur.id=0; q.push(cur); cnt=0; while (cnt<m) { cur=q.top(); q.pop(); if (cur.id>=n) continue; next.now=cur.now; next.next=cur.now+a[cur.id+1]; next.id=cur.id+1; q.push(next); next.now=cur.next; next.next=cur.next+a[cur.id+1]; next.id=cur.id+1; q.push(next); cnt++; } for (i=0;!q.empty();i++) { a[i]=q.top().now; q.pop(); } sort(a,a+m); printf("%d\n",a[m-1]); } int main() { int Case,i; scanf("%d",&Case); for (i=1;i<=Case;i++) { printf("Case #%d: ",i); bfs(); } return 0; }
时间: 2024-11-11 08:41:12