UVA_102_Ecological Bin Packing

#include<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<string>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;
using std::next_permutation;
using std::stringstream;
using std::sort;
using std::unique;
using std::swap;
int main()
{
    //生成二维数组
    s:vector<vector<int>>bin(3);
    for (vector<int>::size_type i = 0; i < 3; i++)
    {
        bin[i].resize(3);
    }
    //输入
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            int num; cin >> num;
            bin[i][j] = num;
        }
    }
    //使数据横纵都排列成BCG
    for (int i = 0; i <3; i++)
    {
        swap(bin[i][1], bin[i][2]);
    }
    //分别计算B,C,G的垃圾总和
    int Brown_sum = bin[0][0]+bin[1][0]+bin[2][0], 
        Clear_sum = bin[0][1] + bin[1][1] + bin[2][1],
        Green_sum = bin[0][2] + bin[1][2] + bin[2][2];
    string code = "012", code_min = "999";
    int count = 1000000000;
    do
    {
        int sum = Brown_sum - bin[0][code[0] - '0']
             +Clear_sum - bin[1][code[1] - '0']
        +Green_sum- bin[2][code[2] - '0'];
        
        if (count > sum)
        {
            count = sum;
            code_min = code;
        }
        else if (count == sum)
        {
            code_min = code_min < code ? code_min : code;
        }
    } while (next_permutation(code.begin(), code.end()));
    for (int i = 0; i < 3; i++)
    {
        switch (code_min[i])
        {
        case '0':cout << 'B'; break;
        case '1':cout << 'C'; break;
        case '2':cout << 'G'; break;
        }
    }
    cout << ' ';
    cout << count << endl;
    cin.get();
    if (cin.peek() != EOF)
    {
        goto s;
    }
    return 0;
}

你可能感兴趣的:(uva,greedy)