分享
 
 
 

v1-htm01\102.htm Ecological Bin Packing

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

Ecological Bin Packing

BackgroundBin packing, or the placement of objects of certain weights into different bins subject to certain constraints, is an historically interesting problem. Some bin packing problems are NP-complete but are amenable to dynamic programming solutions or to approximately optimal heuristic solutions.

In this problem you will be solving a bin packing problem that deals with recycling glass.

The ProblemRecycling glass requires that the glass be separated by color into one of three categories: brown glass, green glass, and clear glass. In this problem you will be given three recycling bins, each containing a specified number of brown, green and clear bottles. In order to be recycled, the bottles will need to be moved so that each bin contains bottles of only one color.

The problem is to minimize the number of bottles that are moved. You may assume that the only problem is to minimize the number of movements between boxes.

For the purposes of this problem, each bin has infinite capacity and the only constraint is moving the bottles so that each bin contains bottles of a single color. The total number of bottles will never exceed 2^31.

The InputThe input consists of a series of lines with each line containing 9 integers. The first three integers on a line represent the number of brown, green, and clear bottles (respectively) in bin number 1, the second three represent the number of brown, green and clear bottles (respectively) in bin number 2, and the last three integers represent the number of brown, green, and clear bottles (respectively) in bin number 3. For example, the line 10 15 20 30 12 8 15 8 31

indicates that there are 20 clear bottles in bin 1, 12 green bottles in bin 2, and 15 brown bottles in bin 3.

Integers on a line will be separated by one or more spaces. Your program should process all lines in the input file.

The OutputFor each line of input there will be one line of output indicating what color bottles go in what bin to minimize the number of bottle movements. You should also print the minimum number of bottle movements.

The output should consist of a string of the three upper case characters 'G', 'B', 'C' (representing the colors green, brown, and clear) representing the color associated with each bin.

The first character of the string represents the color associated with the first bin, the second character of the string represents the color associated with the second bin, and the third character represents the color associated with the third bin.

The integer indicating the minimum number of bottle movements should follow the string.

If more than one order of brown, green, and clear bins yields the minimum number of movements then the alphabetically first string representing a minimal configuration should be printed.

Sample Input

1 2 3 4 5 6 7 8 9

5 10 5 20 10 5 10 20 10

Sample Output

BCG 30

CBG 50

// Ecological Bin Packing.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include "fstream"

#include "string"

using namespace std;

struct bin

{

int clear;

int grean;

int brown;

};

unsigned int count_sept(char str[],const bin in[] )

{

bin bins[3];

memcpy(bins,in,sizeof(bin)*3);

int count = 0;

int j,k;

j = 1;

k = 2;

for( int i = 0; i < 3; i++)

{

switch(str[i])

{

case 'C':

bins[i].clear += bins[j].clear;

count += bins[j].clear;

bins[j].clear = 0;

bins[i].clear += bins[k].clear;

count += bins[k].clear;

bins[k].clear = 0;

break;

case 'B':

bins[i].brown += bins[j].brown;

count += bins[j].brown;

bins[j].brown = 0;

bins[i].brown += bins[k].brown;

count += bins[k].brown;

bins[k].brown = 0;

break;

case 'G':

bins[i].grean += bins[j].grean;

count += bins[j].grean;

bins[j].grean = 0;

bins[i].grean += bins[k].grean;

count += bins[k].grean;

bins[k].grean = 0;

break;

}

j +=1;

j %=3;

k +=1;

k %=3;

}

return count;

}

int main(int argc, char* argv[])

{

bin bins[3];

ifstream fin("in.dat");

ofstream fout("out.txt");

string strbin;

unsigned int min_count ;

unsigned int cout;

while(!fin.eof())

{

int ic = 0;

int ig = 0;

int ib = 0;

min_count = (int)-1;

for(int i = 0; i < 3; i++)

{

fin >>bins[i].brown;

fin >>bins[i].grean;

fin >>bins[i].clear;

}

min_count = count_sept("BCG",bins);

strbin = "BCG";

cout = count_sept("BGC",bins);

if(min_count > cout )

{

min_count = cout;

strbin = "BGC";

}

if(min_count == cout )

{

if(strbin > "BGC")

strbin = "BGC";

}

cout = count_sept("GBC",bins);

if(min_count > cout )

{

min_count = cout;

strbin = "GBC";

}

if(min_count == cout )

{

if(strbin > "GBC")

strbin = "GBC";

}

cout = count_sept("GCB",bins);

if(min_count > cout )

{

min_count = cout;

strbin = "GCB";

}

if(min_count == cout )

{

if(strbin > "GCB")

strbin = "GCB";

}

cout = count_sept("CBG",bins);

if(min_count > cout )

{

min_count = cout;

strbin = "CBG";

}

if(min_count == cout )

{

if(strbin > "CBG")

strbin = "CBG";

}

cout = count_sept("CGB",bins);

if(min_count > cout )

{

min_count = cout;

strbin = "CGB";

}

if(min_count == cout )

{

if(strbin > "CGB")

strbin = "CGB";

}

fout << strbin << " " << min_count << endl;

}

return 0;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有