UVA 10603 Fill (隐式图遍历)

 

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


题意:给定三个容器。第三个容器中装满了水。给定一个指定量的水。要求用3个容器倒出这样的水。最少要转移几升水。

如果倒不出来,在在指定量的水-1继续查询。

思路:状态记录的广搜。开一个二维数组存下前两杯水的状态(因为前两杯水确定了第三杯水就确定了所以只需要开二维数组。然后进行广搜,把能倒出的水量进行标记(标记的值为最小转移的水量)。最后在从指定水量往下找到。如果找到一个有标记过的水量即该水量是可以倒出的最大水量。

UVA上改数据了,重写一发!

代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;

const int N = 205;

int s, n, m, vis[N][N];

struct State {
	int a, b, val;
	State() {}
	State(int a, int b, int val) {
		this->a = a;
		this->b = b;
		this->val = val;
	}
};

void add(int a, int b, int val, queue<State> &Q) {
	if (vis[a][b] > val) {
		vis[a][b] = val;
		Q.push(State(a, b, val));
	}
}

const int INF = 0x3f3f3f3f;

void solve() {
	memset(vis, INF, sizeof(vis));
	queue<State> Q;
	Q.push(State(0, 0, 0));
	vis[0][0] = 0;
	while (!Q.empty()) {
		State u = Q.front();
		Q.pop();
		int us = s - u.a - u.b;
		int ns, na, nb, val;
		ns = max(0, us - (n - u.a));
		val = u.val + us - ns;
		na = us - ns + u.a;
		nb = u.b;
		add(na, nb, val, Q);
		ns = max(0, us - (m - u.b));
		val = u.val + us - ns;
		na = u.a;
		nb = us - ns + u.b;
		add(na, nb, val, Q);
		na = max(0, u.a - (m - u.b));
		val = u.val + u.a - na;
		nb = u.a - na + u.b;
		add(na, nb, val, Q);
		na = max(0, u.a - (s - us));
		val = u.val + u.a - na;
		nb = u.b;
		add(na, nb, val, Q);
		nb = max(0, u.b - (s - us));
		val = u.val + u.b - nb;
		na = u.a;
		add(na, nb, val, Q);
		nb = max(0, u.b - (n - u.a));
		val = u.val + u.b - nb;
		na = u.b - nb + u.a;
		add(na, nb, val, Q);
	}
}

int t, d;

int main() {
	scanf("%d", &t);
	while (t--) {
		scanf("%d%d%d%d", &n, &m, &s, &d);
		solve();
		int i;
		int ans;
		for (i = d; i >= 0; i--) {
			ans = INF;
			for (int j = 0; j <= s - i; j++) {
				int k = s - i - j;
				ans = min(ans, vis[i][j]);
				ans = min(ans, vis[i][k]);
				ans = min(ans, vis[j][k]);
				ans = min(ans, vis[j][i]);
				ans = min(ans, vis[k][i]);
				ans = min(ans, vis[k][j]);
			}
			if (ans != INF) break;
		}
		printf("%d %d\n", ans, i);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值