UVa 10603 FILL (隐式图的搜索)(DEBUGING)

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <cmath>
using namespace std;
int vis[210][210],ans[210];
struct State
{
	int  v[3],dist;
	bool operator <	(const State &rhs) const 
	{
		return dist>rhs.dist;
	}
};
void updata_ans(State u)
{
	for(int i=0;i<3;i++)
	{
		int d=u.v[i];
		if(ans[d]==-1||u.dist<ans[d]) ans[d]=u.dist;
	}
}
void BFS(int a,int b,int c,int d)
{
	memset(vis,0,sizeof(vis));
	memset(ans,-1,sizeof(ans));
	int cup[3];
	cup[0]=a,cup[1]=b,cup[2]=c;
	priority_queue <State> q;
	State start;
	start.v[0]=0,start.v[1]=0,start.v[2]=c;
	q.push(start);
	vis[0][0]=1;
	while(!q.empty())
	{
		State u=q.top();
		q.pop();
		updata_ans(u);
		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]==cup[j]) continue;
					int amount=min(cup[j],u.v[j]+u.v[i])-u.v[j];
					State u2;
					memcpy(&u2,&u,sizeof(u));
					u2.v[i]-=amount;
					u2.v[j]+=amount;
					if(vis[u2.v[0]][u2.v[1]]==0)
					{
						vis[u2.v[0]][u2.v[1]]=1;
						q.push(u2);
					}
				}
			}
		}
	}
	while(d>=0)
	{
		if(ans[d]>=0)
		{
			cout<<ans[d]<<" "<<d<<endl;
			return;
		}
		d--;
	}
}
int main()
{
	freopen("input.txt","r",stdin);
	int T;
	cin>>T;
	while(T--)
	{
		int a,b,c,d;
		cin>>a>>b>>c>>d;
		BFS(a,b,c,d);
	}
	return 0;
}

你可能感兴趣的:(UVa 10603 FILL (隐式图的搜索)(DEBUGING))