首页 技术 正文
技术 2022年11月9日
0 收藏 708 点赞 2,925 浏览 2607 个字

https://vjudge.net/problem/UVA-10603

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

 #include<iostream> #include<cstdio> #include<queue> #include<cstring> using namespace std; ; struct Node{     int x,y,z;     int water;     bool operator<(const Node& b)const{         return water>b.water;     } }; int a,b,c,d,ans[maxn],done[maxn][maxn]; void init(){     memset(ans,-,sizeof(ans));     memset(done,,sizeof(done)); } int main() {     int T;     scanf("%d",&T);     while(T--){         priority_queue<Node> Q;         init();         scanf("%d %d %d %d",&a,&b,&c,&d);         Node start=(Node){,,c,};         Q.push(start);         while(!Q.empty()){             Node r=Q.top();Q.pop();             ||r.water<ans[r.x]) ans[r.x]=r.water;             ||r.water<ans[r.y]) ans[r.y]=r.water;             ||r.water<ans[r.z]) ans[r.z]=r.water;             done[r.x][r.y]=;             ) break;             int change;             change=min(r.x,b-r.y);             if(change&&!done[r.x-change][r.y+change]) Q.push((Node){r.x-change,r.y+change,r.z,r.water+change});             change=min(r.x,c-r.z);             if(change&&!done[r.x-change][r.y]) Q.push((Node){r.x-change,r.y,r.z+change,r.water+change});             change=min(r.y,a-r.x);             if(change&&!done[r.x+change][r.y-change]) Q.push((Node){r.x+change,r.y-change,r.z,r.water+change});             change=min(r.y,c-r.z);             if(change&&!done[r.x][r.y-change]) Q.push((Node){r.x,r.y-change,r.z+change,r.water+change});             change=min(r.z,a-r.x);             if(change&&!done[r.x+change][r.y]) Q.push((Node){r.x+change,r.y,r.z-change,r.water+change});             change=min(r.z,b-r.y);             if(change&&!done[r.x][r.y+change]) Q.push((Node){r.x,r.y+change,r.z-change,r.water+change});         }         ){             ){                 printf("%d %d\n",ans[d],d);                 break;             }             --d;         }     }     ; }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903