uva10603

 

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 secondjug are initially empty, while the third

is completely filled with water. It is allowed to pour waterfrom one jug into another until either the first one is empty or the second oneis full. This operation can be performed zero, one or more times.

 

You are to write a program that computes the least totalamount of water that needs to be poured; so that at least one of the jugscontains exactly d liters of water (d is a positive integer not greater than200). If it is not possible to measure d liters this way your program shouldfind 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 theleast total amount of poured water needed to produce d' liters in at least oneof 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 lineof input containing four space separated integers - a, b, c and d.

 
Output

The output consists of two integers separated by a singlespace. The first integer equals the least total amount (the sum of all watersyou 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 theclosest 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

 

 

Problem source: Bulgarian National Olympiad in Informatics2003

Problem submitter: Ivaylo Riskov

Problem solution: Ivaylo Riskov, Sadrul Habib Chowdhury

做这题费了老长时间,主要是用来构思上,起初是想到刘汝佳书上的经典倒水问题,由于之前没做过这类题,就把书上的例题先敲了出来,但是这个题目与那个题目稍有不同,

就是状态变量是水的移动量,起初想的是直接bfs,不过,发现直接对水量bfs的话,会忽视一些状态量,于是又想到使用优先级队列对水量进行处理。便ac了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define N 220
using namespace std;
typedef struct
{
    int a,b,v;
} Node;
int a,b,c,d,C[N],vis[N][N];
struct cmp
{
    bool operator()(const Node&a,const Node&b)
    {
        return a.v>b.v;
    }
};
Node *q=new Node[N*N];
int bfs()
{
    q[0].a=0,q[0].b=0,q[0].v=0;
    priority_queue<Node,vector<Node>,cmp>Q(q,q+1);
    while(!Q.empty())
    {
        Node u=Q.top();
        int x=u.a,y=u.b,z=c-x-y,v=u.v;
        int x0,y0,v0,X[10],Y[10],V[10],k=0;
        Q.pop();
        if(x+y>=a)
        {
            x0=a;
            y0=x+y-a;
            v0=a-x;
        }
        else
        {
            x0=x+y;
            y0=0;
            v0=y;
        }
        X[k]=x0,Y[k]=y0,V[k++]=v0;
        if(x+z>=a)
        {
            x0=a;
            y0=y;
            v0=a-x;
        }
        else
        {
            x0=x+z;
            y0=y;
            v0=z;
        }
        X[k]=x0,Y[k]=y0,V[k++]=v0;
        if(y+x>=b)
        {
            x0=x+y-b;
            y0=b;
            v0=b-y;
        }
        else
        {
            x0=0;
            y0=x+y;
            v0=x;
        }
        X[k]=x0,Y[k]=y0,V[k++]=v0;
        if(y+z>=b)
        {
            x0=x;
            y0=b;
            v0=b-y;
        }
        else
        {
            x0=x;
            y0=y+z;
            v0=z;
        }
        X[k]=x0,Y[k]=y0,V[k++]=v0;
        x0=0,y0=y,v0=x;
        X[k]=x0,Y[k]=y0,V[k++]=v0;
        x0=x,y0=0,v0=y;
        X[k]=x0,Y[k]=y0,V[k++]=v0;
        for(int i=0; i<k; i++)
        {
            int o=X[i],t=Y[i];
            if(!vis[o][t])
            {
                vis[o][t]=1;
                if(C[o]==-1)C[o]=v+V[i];
                if(C[t]==-1)C[t]=v+V[i];
                if(C[c-o-t]==-1)C[c-o-t]=v+V[i];
                if(C[d]!=-1)return 1;
                else
                {
                    Node m;
                    m.a=o,m.b=t,m.v=v+V[i];
                    Q.push(m);
                }
            }
        }
    }
}
int main()
{
   // freopen("1.txt","r",stdin);
    int n;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>a>>b>>c>>d;
        memset(vis,0,sizeof(vis));
        memset(C,-1,sizeof(C));
        C[0]=C[c]=0;
        vis[0][0]=1;
        bfs();
        for(int j=d; j>=0; j--)
            if(C[j]!=-1)
            {
                cout<<C[j]<<' '<<j<<endl;
                break;
            }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值