博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
B - Fill
阅读量:6659 次
发布时间:2019-06-25

本文共 3348 字,大约阅读时间需要 11 分钟。

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit
Appoint description:
 

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

Sample Output

2

2 3 4 2

96 97 199 62

2 2

9859 62

 

Problem source: Bulgarian National Olympiad in Informatics 2003

Problem submitter: Ivaylo Riskov

Problem solution: Ivaylo Riskov, Sadrul Habib Chowdhury

倒水问题   状态bfs搜索

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 using namespace std;15 const int INF=0x5fffffff;16 const int EXP=1e-5;17 const int MS=205;18 struct node19 {20 int v[3],dist; //这里的dist表示到达这个状态所倒的水量21 bool operator < (const node &a)const22 {23 return dist>a.dist;24 }25 };26 27 int vis[MS][MS],cap[3],ans[MS];28 29 void updata(const node &u)30 {31 for(int i=0;i<3;i++)32 {33 int d=u.v[i]; // 对每个体积进行更新34 if(ans[d]<0||u.dist
que;45 node start;46 start.dist=0;47 start.v[0]=0;48 start.v[1]=0;49 start.v[2]=c;50 que.push(start);51 while(!que.empty())52 {53 node u=que.top();54 que.pop();55 updata(u);56 if(ans[d]>=0)57 break;58 for(int i=0;i<3;i++)59 for(int j=0;j<3;j++) //i-->j;60 if(i!=j)61 {62 if(u.v[i]==0||u.v[j]==cap[j])63 continue; //i没有水 or j已经满了64 int cnt=min(cap[j],u.v[i]+u.v[j])-u.v[j];65 node t;66 memcpy(&t,&u,sizeof(u)); // t=u;67 t.dist=u.dist+cnt;68 t.v[i]-=cnt;69 t.v[j]+=cnt;70 if(!vis[t.v[0]][t.v[1]])71 {72 vis[t.v[0]][t.v[1]]=1;73 que.push(t);74 }75 }76 }77 while(d>=0)78 {79 if(ans[d]>=0)80 {81 printf("%d %d\n",ans[d],d);82 return ;83 }84 d--;85 }86 }87 88 int main()89 {90 int T,a,b,c,d;91 scanf("%d",&T);92 while(T--)93 {94 scanf("%d%d%d%d",&a,&b,&c,&d);95 solve(a,b,c,d);96 }97 return 0;98 }

 

转载于:https://www.cnblogs.com/767355675hutaishi/p/4330524.html

你可能感兴趣的文章
大数据和云计算的鞍马情-【软件和信息服务】2014.08
查看>>
技术变成客户才值钱
查看>>
Powershell管理系列(四十)PowerShell查询和解锁AD账号(改进后,只发一次邮件)
查看>>
大型企业云化2.0的深度思考与展望
查看>>
linux-shell面试题 之三
查看>>
高富帅之 真正的人才与价值
查看>>
防止恶意解析——禁止通过IP直接访问网站
查看>>
FTP文件传输服务
查看>>
Can't open the mysql.plugin table. Please run mysql
查看>>
Tengine 常用模块使用介绍
查看>>
Python回顾与整理4:序列1—字符串
查看>>
CentOS7 安装执行 VmwareCore
查看>>
MSSQLServer将远端数据库保存到本地
查看>>
Outlook 2013连接到Office 365时缓存模式与联机模式下的流量问题
查看>>
关于测试行业的零星思考
查看>>
Wireshark系列之3 路由过程抓包分析
查看>>
链路捆绑---LACP(手动方式)
查看>>
15-Windows Server 2012 新特性 ---- 存储和文件服务
查看>>
数据服务系统的定位
查看>>
实战:使用IPSec保护服务器安全
查看>>