UVA-10603 Fill(状态图的BFS+贪心)

本文详细解析了UVA-10603 Fill问题,通过使用优先队列实现BFS算法来解决如何以最少的倒水量使其中一个杯子恰好含有指定水量的问题。介绍了输入输出格式及样例,并提供了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 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

三个杯子的水为当前状态 , 以状态为图的节点 BFS , 通常 BFS 是根据步数决定出队的先后顺序 , 但这题不是问达到最终状态的最少步数 , 而是最少的倒水量 , 所以要根据倒水量决定出队的先后顺序:用优先队列.(本质上也是一种贪心 ? )

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;

struct node{
    int cup[3],dist;
    bool operator < (const node& a) const{
        return a.dist < dist;
    }    
};
int CUP[3],D;
priority_queue<node> q;//状态的优先队列,按dist小根排序
int ans[205];//每个 d'对应的 最少 pour
bool vis[205][205]; //利用前两个cup的水量确定是哪一个状态

void update_ans(node now){
    for(int i = 0; i < 3; i++)
        if(ans[now.cup[i]] < 0/* || now.dist < ans[now.cup[i]]*/)//如未到达过此状态,更新ans.
          //因为是按照倒水量递增的方式出队,所以原书第二个条件多余
            ans[now.cup[i]] = now.dist;
}

void update_q(node now){
    for(int i = 0; i < 3; i++)
    for(int j = 0; j < 3; j++){
        if(j == i) continue;
        int amount = min(now.cup[i], CUP[j]-now.cup[j]);//把i倒完或者把j倒满
        node x = now;
        x.dist += amount;
        x.cup[i] -= amount;
        x.cup[j] += amount;
        if(!vis[x.cup[0]][x.cup[1]]) {
            vis[x.cup[0]][x.cup[1]] = 1;
            q.push(x);
        }
    }
}
int main(){
    int N;scanf("%d",&N);
    while(N--){
        while(!q.empty()) q.pop();
        memset(ans,-1,sizeof(ans));
        memset(vis,0,sizeof(vis));
        scanf("%d%d%d%d",&CUP[0],&CUP[1],&CUP[2],&D);
        node start;
        start.cup[0]=start.cup[1]=start.dist=0;
        start.cup[2] = CUP[2];
        q.push(start);
        while(!q.empty()) {
            node now = q.top(); q.pop();
            update_ans(now);
            if(ans[D] >= 0) break;
            update_q(now);
        }
        for(int i = D; i >= 0; i--){
            if(ans[i] < 0) continue;
            printf("%d %d\n",ans[i],i);
            break;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值