码迷,mamicode.com
首页 > 其他好文 > 详细

洛谷P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

时间:2017-10-10 19:03:40      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:oss   several   ssi   class   usaco   figure   logs   lines   ret   

P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

题目描述

A little known fact about Bessie and friends is that they love stair climbing races. A better known fact is that cows really don‘t like going down stairs. So after the cows finish racing to the top of their favorite skyscraper, they had a problem. Refusing to climb back down using the stairs, the cows are forced to use the elevator in order to get back to the ground floor.

The elevator has a maximum weight capacity of W (1 <= W <= 100,000,000) pounds and cow i weighs C_i (1 <= C_i <= W) pounds. Please help Bessie figure out how to get all the N (1 <= N <= 18) of the cows to the ground floor using the least number of elevator rides. The sum of the weights of the cows on each elevator ride must be no larger than W.

给出n个物品,体积为w[i],现把其分成若干组,要求每组总体积<=W,问最小分组。(n<=18)

输入输出格式

输入格式:

 

  • Line 1: N and W separated by a space.

  • Lines 2..1+N: Line i+1 contains the integer C_i, giving the weight of one of the cows.

 

输出格式:

 

  • Line 1: A single integer, R, indicating the minimum number of elevator rides needed.

  • Lines 2..1+R: Each line describes the set of cows taking

one of the R trips down the elevator. Each line starts with an integer giving the number of cows in the set, followed by the indices of the individual cows in the set.

 

输入输出样例

输入样例#1:
4 10 
5 
6 
3 
7 
输出样例#1:
3 
2 1 3 
1 2 
1 4 

说明

There are four cows weighing 5, 6, 3, and 7 pounds. The elevator has a maximum weight capacity of 10 pounds.

We can put the cow weighing 3 on the same elevator as any other cow but the other three cows are too heavy to be combined. For the solution above, elevator ride 1 involves cow #1 and #3, elevator ride 2 involves cow #2, and elevator ride 3 involves cow #4. Several other solutions are possible for this input.

技术分享
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 20
int n,m,a[maxn],b[maxn],mid;
bool flag=0;
void dfs(int pos,int num){
    if(pos==n+1){
        flag=1;
        return;
    }
    if(flag)return;
    for(int i=1;i<=num;i++){
        if(m-b[i]>=a[pos]){
            b[i]+=a[pos];
            dfs(pos+1,num);
            b[i]-=a[pos];
        }
    }
    if(num==mid)return;
    b[num+1]=a[pos];
    dfs(pos+1,num+1);
    b[num+1]=0;
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    int ans=n,l=1,r=n;
    while(l<=r){
        mid=(l+r)>>1;
        memset(b,0,sizeof(b));
        flag=0;
        dfs(1,0);
        if(flag)ans=mid,r=mid-1;
        else l=mid+1;
    }
    printf("%d",ans);
}
100分 迭代加深搜索(二分深度)

 

洛谷P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

标签:oss   several   ssi   class   usaco   figure   logs   lines   ret   

原文地址:http://www.cnblogs.com/thmyl/p/7646886.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!