【Jason's_ACM_解题报告】Fill

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
2
2 3 4 2
96 97 199 62

Sample Output
2 2
9859 62
 

仍然是深化对“状态”的理解,我们可以BFS模拟倒水的过程,但是队列是按照水量的大小来出队的。如果我们知道了两个jug的水量,那么另外一个的也就确定了,所以我们可以开辟一个vis[][]数组,来标记当前状态是否得到过,总共有201×201个状态,空间能够承受。

但此题正如Liu所说,尚无法证明此算法的正确性。并且,我也仅仅是想锻炼一下搜索能力,所以正确性的证明待日后回来再证。


附代码如下:
#include<cstdio> 
#include<cstring>

#include<queue>

using namespace std;

#define MAXV (200+5)
#define CLR(x,y) (memset(x,y,sizeof(x)))

struct NODE{
	int v[3],dist;
	bool operator < (const NODE& rhs)const{return dist>rhs.dist;}
};

int vol[3];
int ans[MAXV];
bool vis[MAXV][MAXV];

void update_ans(const NODE& n){
	for(int i=0;i<3;i++){
		int d=n.v[i];
		if(ans[d]==-1||n.dist<ans[d])ans[d]=n.dist;
	}
}

void solve(int a,int b,int c,int d){
	vol[0]=a;vol[1]=b;vol[2]=c;
	CLR(vis,false);
	CLR(ans,-1);
	priority_queue< NODE > q;
	
	NODE st;
	st.v[0]=0;st.v[1]=0;st.v[2]=c;st.dist=0;
	q.push(st);
	vis[0][0]=true;
	while(!q.empty()){
		NODE u=q.top();q.pop();
		update_ans(u);
		if(ans[d]>=0)break;
		
		for(int i=0;i<3;i++){
			for(int j=0;j<3;j++){
				if(i!=j){
					if(u.v[i]==0||u.v[j]==vol[j])continue;
					int amount=min(vol[j],u.v[i]+u.v[j])-u.v[j];
					NODE u2;
					memcpy(&u2,&u,sizeof(u));
					u2.v[i]=u.v[i]-amount;
					u2.v[j]=u.v[j]+amount;
					u2.dist=u.dist+amount;
					if(!vis[u2.v[0]][u2.v[1]]){
						vis[u2.v[0]][u2.v[1]]=true;
						q.push(u2);
					}
				}
			}
		}
	}
	
	while(d>=0){
		if(ans[d]>=0){printf("%d %d\n",ans[d],d);return;}
		d--;
	}
}

int main(){
	int T,a,b,c,d;
	scanf("%d",&T);
	while(T--){
		scanf("%d%d%d%d",&a,&b,&c,&d);
		solve(a,b,c,d);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值