参考的例题写的刘汝佳,就是把每个状态遍历一遍
注意:
bool operator<(const st& rhs)const
{
return pour > rhs.pour;
}
#include<queue>
#include<algorithm>
using namespace std;
#include<stdio.h>
#include<string.h>
struct st
{
int water[3];
int pour;
bool operator<(const st& rhs)const
{
return pour > rhs.pour;
}
};
int vis[250][250];
int ans[250];
int cup[3];
int wanted;
int main()
{
//
freopen("input.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d",cup,cup+1,cup+2,&wanted);
priority_queue<st> que;
st temp;
temp.water[0]=temp.water[1]=0;
temp.water[2]=cup[2];
temp.pour=0;
que.push(temp);
vis[0][0]=1;
memset(ans,-1,sizeof(ans));
memset(vis,0,sizeof(vis));
while(!que.empty())
{
temp=que.top();
que.pop();
for(int i=0;i<3;i++)
{
if(ans[temp.water[i]]==-1||temp.pour<ans[temp.water[i]])
ans[temp.water[i]]=temp.pour;
}
int amount;
st next;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(i!=j)
{
amount=min(temp.water[i],cup[j]-temp.water[j]);
memcpy(&next,&temp,sizeof(temp));
next.water[i]-=amount;
next.water[j]+=amount;
next.pour+=amount;
if(!vis[next.water[0]][next.water[1]])
{
que.push(next);
vis[next.water[0]][next.water[1]]=1;
}
}
}
}
}
int d=wanted;
while(ans[d]==-1)
d--;
printf("%d %d\n",ans[d],d);
}
}