经典的倒水问题,求最接近的可得到的水量d‘ d'<=d 和需要倒的水量
因为a,b,c最大只有200,所以总的状态只有8,000,000,而又因为总水量恒定,所以状态量只有40,000,bfs遍历即可。
/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#define INF 1E9
using namespace std;
struct node
{
int a[3],p;
};
int cost[202][202];
int Min,up,push;
int A[3],d;
node now,next;
queue<node> q;
int main()
{
int T,i,j,k,t,*a;
bool flag=1;
scanf("%d",&T);
while(T--)
{
while(!q.empty())q.pop();
memset(cost,0,sizeof(cost));
flag=1;Min=INF;
scanf("%d%d%d%d",&A[0],&A[1],&A[2],&d);
if(A[2]==d||d==0){printf("0 %d\n",d);continue;}
now.a[0]=0;now.a[1]=0;now.a[2]=A[2];
q.push(now);cost[0][0]=1;
while(!q.empty()&&flag)
{
now=q.front();q.pop();
a=now.a;
for(i=0;i<3&&flag;i++)//倒出杯
{
if(now.a[i]==0)continue;
for(j=0;j<3&&flag;j++)//接受杯,分别是6种倒水情况
{
if(i==j||a[j]==A[j])continue;
next.a[i]=max(a[i]-(A[j]-a[j]),0);
next.a[j]=a[j]+a[i]-next.a[i];
next.a[3-i-j]=a[3-i-j];
next.p=cost[now.a[0]][now.a[1]]+a[i]-next.a[i];
if(cost[next.a[0]][next.a[1]])continue;
for(k=0;k<3;k++)
{
t=d-next.a[k];
if(t<Min&&t>=0){Min=t;push=next.p;up=next.a[k];}
if(Min==0){flag=0;break;}
}
cost[next.a[0]][next.a[1]]=next.p;
q.push(next);
}
}
}
printf("%d %d\n",push-1,up);
}
}