UVA-10603-Fill(BFS+优先队列)

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


Sample Output


2

2 3 4 2

96 97 199 62


2 2

9859 62

思路:标记三个杯子的水量,用优先队列的BFS。

#include <cstdio>
#include <queue>
#define min(A,B)(A<B?A:B)
#define INF 999999999
using namespace std;

struct S{
int x,y,z,sum;

bool operator<(const S &p) const
{
    return sum>p.sum;
}

}t;

int a,b,c,d,ans[201];
bool vis[201][201][201];

int main()
{
    int T,i,j,k,x,y,z,sum;

    scanf("%d",&T);

    while(T--)
    {
        scanf("%d%d%d%d",&a,&b,&c,&d);

        for(i=0;i<=a;i++) for(j=0;j<=b;j++) for(k=0;k<=c;k++) vis[i][j][k]=0;

        for(i=0;i<=d;i++) ans[i]=INF;

        vis[0][0][c]=1;

        t.x=0;
        t.y=0;
        t.z=c;
        t.sum=0;

        priority_queue<S>que;

        que.push(t);

        while(!que.empty())
        {
            t=que.top();
            que.pop();

            x=t.x;
            y=t.y;
            z=t.z;
            sum=t.sum;

            ans[x]=min(ans[x],sum);
            ans[y]=min(ans[y],sum);
            ans[z]=min(ans[z],sum);

            if(x)
            {
                if(y<b)
                {
                    if(x>=b-y && !vis[x-b+y][b][z])
                    {
                        t.x=x-b+y;
                        t.y=b;
                        t.z=z;
                        t.sum=sum+b-y;

                        vis[x-b+y][b][z]=1;

                        que.push(t);
                    }
                    else if(x<b-y && !vis[0][y+x][z])
                    {
                        t.x=0;
                        t.y=y+x;
                        t.z=z;
                        t.sum=sum+x;

                        vis[0][y+x][z]=1;

                        que.push(t);
                    }
                }

                if(z<c)
                {
                    if(x>=c-z && !vis[x-c+z][y][c])
                    {
                        t.x=x-c+z;
                        t.y=y;
                        t.z=c;
                        t.sum=sum+c-z;

                        vis[x-c+z][y][c]=1;

                        que.push(t);
                    }
                    else if(x<c-z && !vis[0][y][z+x])
                    {
                        t.x=0;
                        t.y=y;
                        t.z=z+x;
                        t.sum=sum+x;

                        vis[0][y][z+x]=1;

                        que.push(t);
                    }
                }
            }

            if(y)
            {
                if(x<a)
                {
                    if(y>=a-x && !vis[a][y-a+x][z])
                    {
                        t.x=a;
                        t.y=y-a+x;
                        t.z=z;
                        t.sum=sum+a-x;

                        vis[a][y-a+x][z]=1;

                        que.push(t);
                    }
                    else if(y<a-x && !vis[x+y][0][z])
                    {
                        t.x=x+y;
                        t.y=0;
                        t.z=z;
                        t.sum=sum+y;

                        vis[x+y][0][z]=1;

                        que.push(t);
                    }
                }

                if(z<c)
                {
                    if(y>=c-z && !vis[x][y-c+z][c])
                    {
                        t.x=x;
                        t.y=y-c+z;
                        t.z=c;
                        t.sum=sum+c-z;

                        vis[x][y-c+z][c]=1;

                        que.push(t);
                    }
                    else if(y<c-z && !vis[x][0][z+y])
                    {
                        t.x=x;
                        t.y=0;
                        t.z=z+y;
                        t.sum=sum+y;

                        vis[x][0][z+y]=1;

                        que.push(t);
                    }
                }
            }

            if(z)
            {
                if(x<a)
                {
                    if(z>=a-x && !vis[a][y][z-a+x])
                    {
                        t.x=a;
                        t.y=y;
                        t.z=z-a+x;
                        t.sum=sum+a-x;

                        vis[a][y][z-a+x]=1;

                        que.push(t);
                    }
                    else if(z<a-x && !vis[x+z][y][0])
                    {
                        t.x=x+z;
                        t.y=y;
                        t.z=0;
                        t.sum=sum+z;

                        vis[x+z][y][0]=1;

                        que.push(t);
                    }
                }

                if(y<b)
                {
                    if(z>=b-y && !vis[x][b][z-b+y])
                    {
                        t.x=x;
                        t.y=b;
                        t.z=z-b+y;
                        t.sum=sum+b-y;

                        vis[x][b][z-b+y]=1;

                        que.push(t);
                    }
                    else if(z<b-y && !vis[x][y+z][0])
                    {
                        t.x=x;
                        t.y=y+z;
                        t.z=0;
                        t.sum=sum+z;

                        vis[x][y+z][0]=1;

                        que.push(t);
                    }
                }
            }
        }

        for(i=d;;i--)
        {
            if(ans[i]<INF)
            {
                printf("%d %d\n",ans[i],i);

                break;
            }
        }
    }
}
时间: 2024-10-10 18:04:42

UVA-10603-Fill(BFS+优先队列)的相关文章

uva 10603 Fill (BFS)

uva 10603 Fill 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

UVA 10603 Fill(正确代码虽然很搓,网上许多代码都不能AC)

在做用户查找时 因为要把查找的结果动态加载和显示,所以,那些html元素要由Ajax动态生成.用户打开查找界面时,有系统推荐的用户,而当用户按条件查找后,查找的结果动态加载和显示.所以考虑到用js来搞. 这个for循环就是移除已有的表单.然后根据Ajax请求过来的数据,动态生成新的表单对象.一定要注意j变量从大往小循环,否则,删除div元素后会引起serchResultLenth=serchResult.children.length;长度的变化(这个问题摸索了好久,才搞定,切记) for(va

UVa 10603 Fill [暴力枚举、路径搜索]

10603 Fill 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 rst and the second jug are initially empty, while the third is completely lled with water. It is allowed to pour water f

UVa 10603 Fill (暴力BFS+优先队列)

题意:给定4个数,a,b,c,d,分别代表空杯子容积为a,b,一个盛满水的杯子容积为c,让你不断倒水,找一个dd,是不是存在某个时刻, 某个杯子里的水dd,和d相同,或者无限接近.让求最少的倒水量和dd(可能和d相同). 析:首先由于没有刻度,如果用数学方法计算,不好算,样例还好算一点,我们观察那个a,b,c都不大于200,挺小的,适合暴力求解. 就是把所有情况都倒一次,倒水就两种倒法,要么把一个杯子倒满,要么就是这个杯子空了,由于水量是固定的,那么确定两个杯子的水量, 那么第三个也就确定了,所

UVA FILL(BFS + 优先队列)

Problem D FILL 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

uva 10603 Fill(倒水问题 BFS)

貌似uva崩了,现在进不去,所以这道题还判断正确与否,其实无所谓了,我这是看的网上的代码,写的基本上一样,唉,没办法,不会做,又看了网上的题解,认真写理解吧还是... 构造了一个结构体,water数组用来保存三个杯子的状态,sum用来保存当前的倒水量,visit数组用来保存状态,以 防他们重复访问,三个杯子只需要两个杯子来判断,第三个已经确定,所以开一个二维数组就可以了...然后用 reach数组来存储所有的倒水量,下标是目标水量,值是一共的倒水量... 只需要一次BFS最多有200*200种状

UVA 10603 Fill(正确代码尽管非常搓,网上很多代码都不能AC)

题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=1544">click here~ 此题我预计是加强过数据,在我纠结了非常久的时候我交了好几份网上的代码不是WA就是TLE.在我非常迷茫的时候我又交了一份,AC了(尽管我用随机数据找到了他代码一个不能过的数据). 给了我信心.然后我拿他的代码用随机数跟我的代码进行測试.再用FC找不同..发现了一个致命的错误.一般来说,BFS或者DFS都是须要

UVa - 10603 - Fill

BFS即可,不过需要注意要求不是步数最少,而是需要水量最少.所以拓展的时候需要取出水量最少的结点进行拓展.用优先队列即可. #include <iostream> #include <cstdio> #include <cstdlib> #include <cctype> #include <cstring> #include <string> #include <sstream> #include <vector&

【路径寻找问题】UVa 10603 - Fill

如家大神书上的例题.第一次接触也是按代码敲得.敲的过程感觉很直观.但自己写估计会写的乱七八糟.以后不能砍得难就不愿意做这种题.否则只能做一些水题了.(PS:48) 紫书 1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<queue> 6 using namespace std; 7 const int maxn =

UVA - 10603 Fill(隐式图搜索)

题目大意:经典的倒水问题.给你三个瓶子,体积为a,b,c. 刚开始a,b是空的,c是满的,现在要求你到出体积为d的水.倒水的规则为,要么倒水方为空,要么接水方满 问倒到容量为d时,倒水的最小体积是多少,如果不能倒出体积为d的水,找出d' < d,最接近d的d'和最小的体积 解题思路:刚才时以为直接bfs,用vis标记一下就结束了,结果WA了.为什么会WA,因为我这样求的是倒水次数最少的,而不是倒水体积最小的,WA是肯定的了 接着将vis数组改成int型的,纪录达到这个状态时倒水的体积,结果可想而