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. 反射demo(拷贝一个对象)

    经过了上一次对反射的初步认知,最近又接触到了后,做了一个小demo,感觉这次带了一点理解去做的,比第一次接触反射好了许多. 上次学习的链接,有一些反射用的基础语句.https://www.cnblog ...

  2. hihoCoder编程练习赛49

    题目1 : 相似颜色 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在CSS中我们可以用井号(#)加6位十六进制数表示一种颜色,例如#000000是黑色,#ff0000 ...

  3. 记录使用Redis和nginx 实现一个简单的负载均衡(FB)

    这两年在博客园看了不少大牛的分享,一直打算能写点什么东西. 之前偶然看见一个利用Redis 当作 Session数据宿主的demo,出处我已经找不到了.后来没事看了看nginx相关的东西.其中负载均衡 ...

  4. js 对象转数组

    function objToArray(array) { var arr = [] for (var i in array) { arr.push(array[i]); } console.log(a ...

  5. JS中的反柯里化( uncurrying)

    反柯里化 相反,反柯里化的作用在与扩大函数的适用性,使本来作为特定对象所拥有的功能的函数可以被任意对象所用.即把如下给定的函数签名, obj.func(arg1, arg2) 转化成一个函数形式,签名 ...

  6. Netty实现一个简单聊天系统(点对点及服务端推送)

    Netty是一个基于NIO,异步的,事件驱动的网络通信框架.由于使用Java提供 的NIO包中的API开发网络服务器代码量大,复杂,难保证稳定性.netty这类的网络框架应运而生.通过使用netty框 ...

  7. Android调用系统图库返回路径

    调用系统图库: Intent intent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI); ...

  8. 打包错误--Error:A problem was found with the configuration of task ':app:packageRelease'.

    解决办法: app目录下的build.gradle文件 将 shrinkResources 的值改为 false 或者直接去掉 shrinkResources true  表示 :打包的时候会去删除一 ...

  9. python安装小结

    一.python下载地址:http://www.activestate.com/activepython/downloads 二.1.没有安装request会出一下错误: 2.解决办法:pip ins ...

  10. DAY3(PYTHON)

    一.or 和and的区别 X OR Y,如果X非0,则为X X OR Y,如果X为真,则为Y 二.continue 跳出当次循环 break 跳出循环 三.#输出1-2+3-4+5-6+......- ...