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

 

Problem source: Bulgarian National Olympiad in Informatics 2003

Problem submitter: Ivaylo Riskov

Problem solution: Ivaylo Riskov, Sadrul Habib Chowdhury



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

// 代表状态结构体
typedef struct v_node
{
	int v_a;
	int v_b;
	int v_c;
	int v_total;	// 代表在该状态之前所用的水量

	bool operator < (const struct v_node &x) const
	{
		return v_total > x.v_total;
	}

}v_node;

// 代表三个杯子水的状态
int state_flag[201][201][201];
// 遍历时的状态队列
priority_queue<v_node> my_queue;

void gen_instance();

int main()
{
//	gen_instance();
	int n;
	scanf("%d", &n);
	int count = 0;
	while(count < n)
	{
		memset(state_flag, 0, sizeof(state_flag));	
		my_queue = priority_queue<v_node>();

		int a_total, b_total, c_total;
		int d_total;
		// 代表最接近d的d'及其用水量
		int d_pre = 0;
		int v_pre_total = 0;
		scanf("%d%d%d%d", &a_total, &b_total, &c_total, &d_total);

		// 生成初始状态节点
		v_node* p = (v_node*)malloc(sizeof(v_node));
		memset(p, 0, sizeof(p));
		p->v_a = 0;
		p->v_b = 0;
		p->v_c = c_total;
		p->v_total = 0;

		state_flag[p->v_a][p->v_b][p->v_c] = 1;
		my_queue.push(*p);

		// 进行广度优先遍历,寻找符合的状态
		int find_flag = 0;
		while(my_queue.size() > 0)
		{
			v_node m = my_queue.top();
			v_node* p = &m;
//			printf("a: %d b: %d c: %d total: %d\n", p->v_a, p->v_b, p->v_c, p->v_total);
			my_queue.pop();
//			printf("a: %d b: %d c: %d total: %d\n", p->v_a, p->v_b, p->v_c, p->v_total);
			// 如果找到符合状态,输出
			if(p->v_a == d_total || p->v_b == d_total || p->v_c == d_total)
			{
				printf("%d %d\n", p->v_total, d_total);
				find_flag = 1;
				break;	
			}	
			// 如果没找到符合状态
			else 
			{
				// 如果可能的话更新d'
				int this_d_pre = 0;
				if(p->v_a < d_total && p->v_a > this_d_pre)
					this_d_pre = p->v_a;
				if(p->v_b < d_total && p->v_b > this_d_pre)
                                        this_d_pre = p->v_b;
				if(p->v_c < d_total && p->v_c > this_d_pre)
                                        this_d_pre = p->v_c;

				if(this_d_pre > d_pre)
				{
					d_pre = this_d_pre;
					v_pre_total = p->v_total;
				}
			
				// 如果可能的话生成新的6个状态
				for(int i = 1; i <= 6; i++)
				{
					// 倒出来的杯子的水量
					int from_v;
					// 倒进去的杯子的水量
					int to_v;
					// 倒进去的杯子的总水量
					int to_total;

					// 1.a倒入b
					if(i == 1)
					{
						from_v = p->v_a;
						to_v = p->v_b;
						to_total = b_total;
					}	
					// 2.a倒入c
					else if(i == 2) 
					{
						from_v = p->v_a;
                                                to_v = p->v_c;
                                                to_total = c_total;
					}
					// 3.b倒入a
                                        else if(i == 3)  
                                        {
                                                from_v = p->v_b;
                                                to_v = p->v_a;
                                                to_total = a_total;
                                        }
					// 4.b倒入c
                                        else if(i == 4)
                                        {
                                                from_v = p->v_b;
                                                to_v = p->v_c;
                                                to_total = c_total;
                                        }
					// 5.c倒入a
                                        else if(i == 5)
                                        {
                                                from_v = p->v_c;
                                                to_v = p->v_a;
                                                to_total = a_total;
                                        }
					// 6.c倒入b
                                        else if(i == 6)
                                        {
                                                from_v = p->v_c;
                                                to_v = p->v_b;
                                                to_total = b_total;
                                        }
					// 检查是否可以倒水,如果可以,模拟倒水
					if(from_v > 0 && to_v < to_total)
					{
						int from_now_v;
						int to_now_v;
						int now_total;
						if(from_v > to_total-to_v)
						{
							from_now_v = from_v - (to_total-to_v);
							to_now_v = to_total;
							now_total = p->v_total + (to_total-to_v);
						}
						else
						{
							from_now_v = 0;
							to_now_v = to_v + from_v;
							now_total = p->v_total + from_v;
						}			
					
						int now_a, now_b, now_c;
						// 1.a倒入b
                                        	if(i == 1)
                                        	{
                                                	now_a = from_now_v;
                                                	now_b = to_now_v;
                                                	now_c = p->v_c;
                                        	}
                                        	// 2.a倒入c
                                        	else if(i == 2)
                                        	{
							now_a = from_now_v;
                                                        now_b = p->v_b;
                                                        now_c = to_now_v;
                                        	}
                                        	// 3.b倒入a
                                        	else if(i == 3)
                                        	{
							now_a = to_now_v;
                                                        now_b = from_now_v;
                                                        now_c = p->v_c;
                                        	}
                                        	// 4.b倒入c
                                        	else if(i == 4)
                                        	{
							now_a = p->v_a;
                                                        now_b = from_now_v;
                                                        now_c = to_now_v;
                                        	}
                                        	// 5.c倒入a
                                        	else if(i == 5)	
						{
							now_a = to_now_v;
                                                        now_b = p->v_b;
                                                        now_c = from_now_v;
                                        	}
                                        	// 6.c倒入b
                                        	else if(i == 6)
                                        	{
							now_a = p->v_a;
                                                        now_b = to_now_v;
                                                        now_c = from_now_v;
                                        	}

						if(state_flag[now_a][now_b][now_c] == 0)
						{
							state_flag[now_a][now_b][now_c] = 1;
							v_node* q = (v_node*)malloc(sizeof(v_node));
							memset(q, 0, sizeof(q));
							q->v_a = now_a;
							q->v_b = now_b;
							q->v_c = now_c;
							q->v_total = now_total;
							my_queue.push(*q);	
//							printf("  new a: %d b: %d c: %d total: %d\n",now_a,now_b,now_c,now_total);
						}	
					}
				}
										
			}
		}

		if(find_flag == 0)
			printf("%d %d\n", v_pre_total, d_pre);
		count++;									
	}
	return 0;		
}


void gen_instance()
{
	int a, b, c, t;
	while(scanf("%d%d%d%d", &a, &b, &c, &t) == 4)
	{
		v_node* p = (v_node*)malloc(sizeof(v_node));
		memset(p, 0, sizeof(p));
		p->v_a = a;
		p->v_b = b;
		p->v_c = c;
		p->v_total = t;
		my_queue.push(*p);	
	}	

	while(my_queue.size() > 0)
	{
		v_node m = my_queue.top();
		my_queue.pop();
		v_node* p = &m;
		printf("this: a: %d b: %d c: %d t: %d\n", p->v_a, p->v_b, p->v_c, p->v_total);
	}
} 

一开始以为直接BFS就可以,发现题目要求找到用最少水量的解,而不是最少倒水次数的解!这点以后要注意,BFS求的是最少步数的解。

后来改用优先队列,每次取最少水量的状态来更新队列,但上述代码一直TLE,不知为何,以后研究。

正确代码链接:

https://github.com/aoapc-book/aoapc-bac2nd/blob/master/ch7/UVa10603.cpp

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值