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. ASPxGridView 添加勾选列--全选 和 后端获取勾的行ID

    一.HTML 代码 <table style="width: 100%;"> <tr> <td> <asp:Button ID=" ...

  2. js 毫秒转天时分秒

    formatDuring: function(mss) { var days = parseInt(mss / (1000 * 60 * 60 * 24)); var hours = parseInt ...

  3. BZOJ1058: [ZJOI2007]报表统计(set)

    Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 4190  Solved: 1420[Submit][Status][Discuss] Descript ...

  4. BDD实战篇 - .NET Core里跑Specflow - 可以跑集成测试和单元测试

    这是<如何用ABP框架快速完成项目 >系列中和DevOps系列文章其中一篇文章.   BDD很赞!比TDD先进很多,能够大大提高编码效率.   上一篇文章说了如何在.NET Core里安装 ...

  5. 快速上手ABP - Angular部分 - 如何最快速度了解相关API。

    不是google,不是angular官网,而是在Visual Studio Code选中这个API对象,鼠标右键,选"Go to Definition" 例子:要想了解FormGr ...

  6. 接口自动化 [授客]基于python+Testlink+Jenkins实现的接口自动化测试框架V3.0

    基于python+Testlink+Jenkins实现的接口自动化测试框架V3.0   by:授客 QQ:1033553122     博客:http://blog.sina.com.cn/ishou ...

  7. 浅谈Kotlin(四):控制流

    浅谈Kotlin(一):简介及Android Studio中配置 浅谈Kotlin(二):基本类型.基本语法.代码风格 浅谈Kotlin(三):类 浅谈Kotlin(四):控制流 本篇介绍Kotlin ...

  8. Android为TV端助力 Service 两种启动方式的区别

    服务不能自己运行,需要通过调用Context.startService()或Context.bindService()方法启动服务.这两个方法都 可以启动Service,但是它们的使用场合有所不同.使 ...

  9. <API自动化测试>Centos-Newman

    一.介绍: 在测试和开发中,有一款API测试工具一直占据着武林盟主的地位,那就是声名远播的Google公司的Postman. Postman原先是Chrome浏览器的一个插件,后面发展成了一个应用程序 ...

  10. 转载:使用redis+flask维护动态代理池

    githu源码地址:https://github.com/Germey/ProxyPool更好的代理池维护:https://github.com/Python3WebSpider/ProxyPool ...