博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA10603-Fill(BFS)
阅读量:5152 次
发布时间:2019-06-13

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

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

你可能感兴趣的文章
sql 触发器删除操作
查看>>
第一次作业
查看>>
《JavaScript高级程序设计》学习笔记——最佳实践
查看>>
【CCPC-Wannafly Winter Camp Day3 (Div1) G】排列(水题)
查看>>
js 中三元运算符的运用
查看>>
DLL函数重定向
查看>>
docker 常用命令
查看>>
Python学习笔记--8.7 函数--可变参数、关键字参数
查看>>
四则运算的代码的改进(三)
查看>>
thinkphp 内置标签volist 控制换行
查看>>
使用存储过程和视图存储配置
查看>>
数据分析告诉我们的四个经验教训
查看>>
ReportView动态加载带参数的RDCL文件及子报表
查看>>
inside tomcat 6, 环境搭建
查看>>
INF6027 Introduction to Data Science Analysis of the UK Police Dataset
查看>>
设计模式(创建型模式-单例模式)
查看>>
超市收银系统_定义商品的父类和各个子类的实现——1
查看>>
LAMP环境搭建(Ubuntu)
查看>>
Java学习笔记(15)
查看>>
New year comes again
查看>>