UVA10603-Fill(BFS)

博客围绕UVA10603倒水问题展开,该问题要求计算最少倒水量使至少一个壶中有指定水量,无解时输出小于指定水量且最接近的有解情况。题解将普通队列换为优先队列,从Dijkstra角度保证正确性,还介绍直接记录所有解来统计答案,代码编写更方便。

Problem UVA10603-Fill

Accept:1162  Submit:10693

Time Limit: 3000 mSec

 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 Ouput

2 2

9859 62

 

题解:倒水问题,原来只写过步数最少的,而这个题问的是倒水量最少。解决方案是将普通的队列换成优先队列,每次优先取出倒水量最少的节点。

这样做的正确性lrj表示不会证,但是可以从Dijkstra上考虑。把状态看成节点,以两个状态之间倒水量作为边权,问题就成了最短路,正确性还是有保证的。

这个题还有一个要求是如果无解,输出小于需要得到的水量并且距离需要得到水量最近的有解的情况。一开始考虑的是维护一个距离,一个最小倒水量,但是不如lrj的做法方便:直接记录所有解,最后统计答案,代码比较好写。

 

 1 #include <bits/stdc++.h>
 2 #define INF 0x3f3f3f3f
 3 using namespace std;
 4 
 5 const int maxn = 200+10;
 6 
 7 struct Node{
 8     int v[3];
 9     int dist;
10     Node() {}
11     bool operator < (const Node &a)const{
12         return dist > a.dist;
13     }
14 };
15 
16 int d,cap[3];
17 int ans[maxn];
18 bool vis[maxn][maxn];
19 
20 void update_ans(const Node &u){
21     for(int i = 0;i < 3;i++){
22         int d = u.v[i];
23         if(ans[d]<0 || u.dist<ans[d]) ans[d] = u.dist;
24     }
25 }
26 
27 void bfs(){
28     Node start;
29     start.v[0] = 0,start.v[1] = 0,start.v[2] = cap[2];
30     start.dist = 0;
31     memset(ans,-1,sizeof(ans));
32     memset(vis,false,sizeof(vis));
33     priority_queue<Node> que;
34     que.push(start);
35     vis[0][0] = true;
36     while(!que.empty()){
37         Node top = que.top();que.pop();
38         update_ans(top);
39         if(ans[d] >= 0) break;
40         for(int i = 0;i < 3;i++){
41             for(int j = 0;j < 3;j++){
42                 if(i == j) continue;
43                 if(top.v[i]==0 || top.v[j]==cap[j]) continue;
44                 int amount = min(top.v[i],cap[j]-top.v[j]);
45                 Node Next = top;
46                 Next.dist += amount;
47                 Next.v[i] -= amount,Next.v[j] += amount;
48                 if(!vis[Next.v[0]][Next.v[1]]){
49                     vis[Next.v[0]][Next.v[1]] = true;
50                     que.push(Next);
51                 }
52             }
53         }
54     }
55     for(int i = d;i >= 0;i--){
56         if(ans[i] >= 0){
57             printf("%d %d\n",ans[i],i);
58             return;
59         }
60     }
61 }
62 
63 int main()
64 {
65     //freopen("input.txt","r",stdin);
66     //freopen("output.txt","w",stdout);
67     int iCase;
68     scanf("%d",&iCase);
69     while(iCase--){
70         for(int i = 0;i < 3;i++){
71             scanf("%d",&cap[i]);
72         }
73         scanf("%d",&d);
74         bfs();
75     }
76     return 0;
77 }

 

转载于:https://www.cnblogs.com/npugen/p/9539061.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值