UVaOJ10603 - Fill

10603 - Fill
Time limit: 3.000 seconds

There are three jugs with a volume of a, b and c liters. (a, b, and c are positive integers not greater than 200). The first and the second jug are initially empty, while the third

is completely filled with water. It is allowed to pour water from one jug into another until either the first one is empty or the second one is full. This operation can be performed zero, one or more times.

 

You are to write a program that computes the least total amount of water that needs to be poured; so that at least one of the jugs contains exactly d liters of water (d is a positive integer not greater than 200). If it is not possible to measure d liters this way your program should find a smaller amount of water d' < d which is closest to d and for which d' liters could be produced. When d' is found, your program should compute the least total amount of poured water needed to produce d' liters in at least one of the jugs.

 

Input

The first line of input contains the number of test cases. In the next T lines, T test cases follow. Each test case is given in one line of input containing four space separated integers - a, b, c and d.

 
Output

The output consists of two integers separated by a single space. The first integer equals the least total amount (the sum of all waters you pour from one jug to another) of poured water. The second integer equals d, if d liters of water could be produced by such transformations, or equals the closest smaller value d' that your program has found.

 

Sample Input

Sample Output

2

2 3 4 2

96 97 199 62

2 2

9859 62

 

Problem source: Bulgarian National Olympiad in Informatics 2003

Problem submitter: Ivaylo Riskov

Problem solution: Ivaylo Riskov, Sadrul Habib Chowdhury

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int volume[3], state[3], minVolume, d, d1;
bool flag, vis[205][205][205];

void ssearch(int tot) {
    for (int i = 0; i < 3; i ++) {
        if (state[i] && state[i] == d) {
            d1 = d;
            if (!flag) minVolume = tot;
            else if (tot < minVolume) minVolume = tot;
            flag = true;
        } else if (!flag && state[i] && state[i] < d){
            if (d - state[i] < d1) {
                d1 = d - state[i];
                minVolume = tot;
            } else if (d - state[i] == d1 && tot < minVolume) {
                minVolume = tot;
            }
        }
    }
    for (int i = 0; i < 3; i ++) {
        for (int j = 0; j < 3; j ++) {\
            int add;
            int tem_i = state[i], tem_j = state[j];
            if (i != j && state[i] && state[j] != volume[j]) {
                if (state[i] >= volume[j] - state[j]) {
                    add = volume[j] - state[j];
                    state[i] -= add;
                    state[j] = volume[j];
                } else {
                    state[j] += state[i];
                    add = state[i];
                    state[i] = 0;
                }
            }
            if (!vis[state[0]][state[1]][state[2]]) {
                vis[state[0]][state[1]][state[2]] = true;
                ssearch(tot + add);
                vis[state[0]][state[1]][state[2]] = false;
            }
            state[i] = tem_i;
            state[j] = tem_j;
        }
    }
}
int main() {
    int T;
    scanf("%d", &T);
    while (T --) {
        scanf("%d%d%d%d", &volume[0], &volume[1], &volume[2], &d);
        state[0] = 0;
        state[1] = 0;
        state[2] = volume[2];
        memset(vis, false, sizeof(vis));
        vis[0][0][volume[2]] = true;
        minVolume = d1 = 1000000;
        ssearch(0);
        if (flag) {
            printf("%d %d\n", minVolume, d1);
        } else {
            printf("%d %d\n", minVolume, d - d1);
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值