10603UVA倒水

参考的例题写的刘汝佳,就是把每个状态遍历一遍

注意:

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);
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值