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的做法方便:直接记录所有解,最后统计答案,代码比较好写。

 #include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std; const int maxn = +; struct Node{
int v[];
int dist;
Node() {}
bool operator < (const Node &a)const{
return dist > a.dist;
}
}; int d,cap[];
int ans[maxn];
bool vis[maxn][maxn]; void update_ans(const Node &u){
for(int i = ;i < ;i++){
int d = u.v[i];
if(ans[d]< || u.dist<ans[d]) ans[d] = u.dist;
}
} void bfs(){
Node start;
start.v[] = ,start.v[] = ,start.v[] = cap[];
start.dist = ;
memset(ans,-,sizeof(ans));
memset(vis,false,sizeof(vis));
priority_queue<Node> que;
que.push(start);
vis[][] = true;
while(!que.empty()){
Node top = que.top();que.pop();
update_ans(top);
if(ans[d] >= ) break;
for(int i = ;i < ;i++){
for(int j = ;j < ;j++){
if(i == j) continue;
if(top.v[i]== || top.v[j]==cap[j]) continue;
int amount = min(top.v[i],cap[j]-top.v[j]);
Node Next = top;
Next.dist += amount;
Next.v[i] -= amount,Next.v[j] += amount;
if(!vis[Next.v[]][Next.v[]]){
vis[Next.v[]][Next.v[]] = true;
que.push(Next);
}
}
}
}
for(int i = d;i >= ;i--){
if(ans[i] >= ){
printf("%d %d\n",ans[i],i);
return;
}
}
} int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int iCase;
scanf("%d",&iCase);
while(iCase--){
for(int i = ;i < ;i++){
scanf("%d",&cap[i]);
}
scanf("%d",&d);
bfs();
}
return ;
}

UVA10603-Fill(BFS)的更多相关文章

  1. UVA-10603 Fill (BFS)

    题目大意:有三个已知体积但不知刻度的杯子,前两个杯子中初始时没有水,第三个装满水,问是否可以倒出d升水,如果倒不出,则倒出一个最大的d’,使得d’<=d,并且在这个过程中要求总倒水量最少. 题目 ...

  2. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  3. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  4. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  5. 【BZOJ5492】[HNOI2019]校园旅行(bfs)

    [HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...

  6. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

  7. 图的 储存 深度优先(DFS)广度优先(BFS)遍历

    图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...

  8. 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)

    一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...

  9. 层层递进——宽度优先搜索(BFS)

    问题引入 我们接着上次“解救小哈”的问题继续探索,不过这次是用宽度优先搜索(BFS). 注:问题来源可以点击这里 http://www.cnblogs.com/OctoptusLian/p/74296 ...

  10. HDU.2612 Find a way (BFS)

    HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...

随机推荐

  1. javascript算法-单链表

    链表相比数组更具灵活性和扩展性.主要有节点数据以及指向节点的指针所构成. 链表中节点的实现[元素和指针]: let Node = function( element ){ this.element = ...

  2. 判断文本是否溢出/hover显示全部

    前言 在工作中我们经常会遇到,文字过多,需要用省略号,并且鼠标hover的时候 还需要 显示全部的文字的需求. 正文 文字过多需要用省略号的实现:上代码啦 .ellipsis { width: 100 ...

  3. vue从入门到进阶:渲染函数 & JSX(八)

    Vue 推荐在绝大多数情况下使用 template 来创建你的 HTML.然而在一些场景中,你真的需要 JavaScript 的完全编程的能力,这就是 render 函数,它比 template 更接 ...

  4. SPOJ7258 SUBLEX - Lexicographical Substring Search(后缀自动机)

    Little Daniel loves to play with strings! He always finds different ways to have fun with strings! K ...

  5. Visualization of Detail Point Set by Local Algebraic Sphere Fitting

    Refers to Dynamic Sampling and Rendering of Algebraic Point Set Surfaces Growing Least Squares for t ...

  6. Android 逆向实战篇(加密数据包破解)

    1. 实战背景由于工作需要,要爬取某款App的数据,App的具体名称此处不便透露,避免他们发现并修改加密逻辑我就得重新破解了. 爬取这款App时发现,抓包抓到的数据是加密过的,如图1所示(原数据较长, ...

  7. sdk manager闪退

    ,1确认好sdk环境变量是否都正确 命令行:android 来验证 2确定jdk是否安装正确 命令行:java   和  javac 都没有问题,就将sdk安装版覆盖安装下不要卸载,不然下载的太慢了 ...

  8. Testlink1.9.17使用方法(第九章 测试结果分析)

    第九章 测试结果分析 QQ交流群:585499566 TestLink根据测试过程中记录的数据,提供了较为丰富的度量统计功能,可以直观的得到测试管理过程中需要进行分析和总结的数据.点击首页横向导航栏中 ...

  9. DAY14(PYTHONS)生成器进阶

    def average(): sum = 0 count = 0 avg = 0 while True: #循环 num = yield avg #遇到yield就停止,防止一开始除数为0导致崩溃 s ...

  10. (网页)习惯了CS回车操作人员,操作BS网页表单也是回车666

    1.第一步把表单,里面需要回车的input,或者是其他的表单按钮给一个clsss,例如下面的$('.cls'); 2.第二步,  把下面的代码复制过去,填写完最后一个自动提交:$("#sav ...