UVA - 10603 Fill (经典倒水问题)

Problem Description

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

题意:有三个杯子它们的容量分别是a,b,c, 并且初始状态下第一个和第二个是空的, 第三个杯子是满水的。可以把一个杯子的水倒入另一个杯子,当然,当被倒的杯子满了或者倒的杯子水完了,就不能继续倒了。
任务是写一个程序计算出用最少的倒水量,使得其中一个杯子里有d升水。如果不能倒出d升水的话,那么找到一个d’ < d ,使得d’ 最接近d
分析思路:
因为共有3个水杯, 根据每一杯的水量v1,v2,v3, 可以得到一个状态,为了方便进行dfs搜索的状态转移,可以用两个两个结构体分别表示三个杯子的容量和状态。然后会有倒水的动作,可以从第1个杯子倒入第2,3个,从第2个倒入第1,3个等等…所以用两个for(切记i!=j) 循环,可以遍历出所有的倒水方案。
然后注意一些不能倒水的条件,比如被倒的杯子是空的,目标杯子是满的,则不进行倒水的动作

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
bool book[201][201];
int ans[201];
int b[3],d;
struct node
{
    int a[3],t;
    bool operator <(const node& z)const
    {
        return t>z.t;
    }
};
void up(node u)
{
    for(int i=0; i<3; i++)
    {
        int z=u.a[i];
        if(ans[z]<0||u.t<ans[z])
            ans[z]=u.t;
    }
}
void bfs(int x,int y,int z,int d)
{
    priority_queue<node> q;
    memset(book,0,sizeof(book));
    memset(ans,-1,sizeof(ans));
    node u;
    u.a[0]=0;
    u.a[1]=0;
    u.a[2]=z;
    u.t=0;
    book[0][0]=1;
    q.push(u);
    node now,next;
    while(!q.empty())
    {
        now=q.top();
        q.pop();
        up(now);
        if(ans[d]>=0)
            break;
        for(int i=0; i<3; i++)
            for(int j=0; j<3; j++)
            {
                if(i!=j)
                {
                    if(now.a[i]==0||now.a[j]==b[j])
                        continue;
                    int minn=min(now.a[i],b[j]-now.a[j]);
                    next=now;
                    next.t=now.t+minn;
                    next.a[i]-=minn;
                    next.a[j]+=minn;
                    if(!book[next.a[0]][next.a[1]])
                    {
                        book[next.a[0]][next.a[1]]=1;
                        q.push(next);
                    }
                }
            }
    }
    while(d>=0)
    {
        if(ans[d]>=0)
        {
            printf("%d %d\n",ans[d],d);
            return ;
        }
        d--;
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d%d",&b[0],&b[1],&b[2],&d);
        bfs(b[0],b[1],b[2],d);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值