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

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

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <sstream>
#include <iomanip>
using namespace std;
const int INF=0x5fffffff;
const int EXP=1e-;
const int MS=;
struct node
{
int v[],dist; //这里的dist表示到达这个状态所倒的水量
bool operator < (const node &a)const
{
return dist>a.dist;
}
}; int vis[MS][MS],cap[],ans[MS]; void updata(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 solve(int a,int b,int c,int d)
{
cap[]=a;cap[]=b;cap[]=c;
memset(vis,,sizeof(vis));
memset(ans,-,sizeof(ans));
priority_queue<node> que;
node start;
start.dist=;
start.v[]=;
start.v[]=;
start.v[]=c;
que.push(start);
while(!que.empty())
{
node u=que.top();
que.pop();
updata(u);
if(ans[d]>=)
break;
for(int i=;i<;i++)
for(int j=;j<;j++) //i-->j;
if(i!=j)
{
if(u.v[i]==||u.v[j]==cap[j])
continue; //i没有水 or j已经满了
int cnt=min(cap[j],u.v[i]+u.v[j])-u.v[j];
node t;
memcpy(&t,&u,sizeof(u)); // t=u;
t.dist=u.dist+cnt;
t.v[i]-=cnt;
t.v[j]+=cnt;
if(!vis[t.v[]][t.v[]])
{
vis[t.v[]][t.v[]]=;
que.push(t);
}
}
}
while(d>=)
{
if(ans[d]>=)
{
printf("%d %d\n",ans[d],d);
return ;
}
d--;
}
} int main()
{
int T,a,b,c,d;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
solve(a,b,c,d);
}
return ;
}

B - Fill的更多相关文章

  1. iOS 2D绘图 (Quartz2D)之路径(stroke,fill,clip,subpath,blend)

    像往常一样 这个系列的博客是跟着大神的脚步来的.按照往例 在此贴出原博客的出处: http://blog.csdn.net/hello_hwc?viewmode=list我对大神的崇拜之情 如滔滔江水 ...

  2. dev_set_draw的fill和margin模式

    注意:分别观察两张填充模式,一种是内部填充,一种是边缘填充.还有一种缺省的填充. Name dev_set_draw — Define the region fill mode. Signature ...

  3. scala 学习之: list.fill 用法

    题目描述: Decode a run-length encoded list. Given a run-length code list generated as specified in probl ...

  4. 急!JDBC问题,发生通信错误。错误位置:Reply.fill()。消息:数据不足。 ERRORCODE=-4499, SQLSTATE=08001

    代码如下:Class.forName("com.ibm.db2.jcc.DB2Driver");Connection conn = DriverManager.getConnect ...

  5. [javascript svg fill stroke stroke-width points polygon属性讲解] svg fill stroke stroke-width points polygon绘制多边形属性并且演示polyline和polygon区别讲解

    <!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...

  6. [javascript svg fill stroke stroke-width points polyline 属性讲解] svg fill stroke stroke-width points polyline 绘制折线属性讲解

    <!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...

  7. [javascript svg fill stroke stroke-width x1 y1 x2 y2 line stroke-opacity fill-opacity 属性讲解] svg fill stroke stroke-width stroke-opacity fill-opacity line绘制线条属性讲解

    <!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...

  8. [javascript svg fill stroke stroke-width x y rect rx ry 属性讲解] svg fill stroke stroke-width rect 绘制具有圆角矩形属性讲解

    <!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...

  9. [javascript svg fill stroke stroke-width rx ry ellipse 属性讲解] svg fill stroke stroke-width ellipse 绘制椭圆属性讲解

    <!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...

  10. [javascript svg fill stroke stroke-width circle 属性讲解] svg fill stroke stroke-width circle 属性 绘制圆形及引入方式讲解

    <!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...

随机推荐

  1. HD2058The sum problem

    The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  2. Spring中的BeanUtils与apache commons中的BeanUtils用法[1]

    1. 前言 在开发过程中,经常遇到把要给一个bean的属性赋给另外一个bean.最笨的方法是每个属性都单独写一个,聪明的方法是应用反射写一个工具方法.考虑到这个需求基本每个程序员都会遇到,那么一定已经 ...

  3. ASP.NET中Request.ApplicationPath、Request.FilePath、Request.Path、.Request.MapPath、Server.MapPath(转载)

    1.Request.ApplicationPath->当前应用的目录   Jsp中, ApplicationPath指的是当前的application(应用程序)的目录,ASP.NET中也是这个 ...

  4. “IPSec 共享密钥”丢失。

    “IPSec 共享密钥”丢失.请验证您的设置并尝试重新连接. 但是这个vpn原来使用时不需要配置 共享的密钥,而且我也没有这个共享的密钥, 搜了下Mac OSX可以修改配置来绕过它. 在/etc/pp ...

  5. Linux下安装protobuf并实现简单的客户端服务器端通信

    http://code.google.com/p/protobuf/downloads/list上可以下载Protobuf的源代码. 安装步骤如下所示: 1>tar -xzf protobuf- ...

  6. xib上拖拽scrollview的自动布局方法

    http://www.cocoachina.com/ios/20150104/10810.html

  7. 组合View Controller时遇到的一点问题

    View Controller的组合应用其实很常见了,比如说Tab bar controller和Navigation view controller的组合使用,像这种一般都是Navigation v ...

  8. 上门洗车App 竟然是块大肥肉!

    http://www.leiphone.com/k-xiche-app-idea.html 打车App.租车App.防违规App我们见得多,但洗车App你一定没听过,之前在一次创业路演上碰到一个做上门 ...

  9. 系列文章--精通CSS.DIV网页样式与布局学习

    精通CSS.DIV网页样式与布局(八)——滤镜的使用 精通CSS.DIV网页样式与布局(七)——制作实用菜单 精通CSS.DIV网页样式与布局(六)——页面和浏览器元素 精通CSS.DIV网页样式与布 ...

  10. C# WinForm使用Aspose.Cells.dll 导出导入Excel/Doc 完整实例教程

    1.添加引用: Aspose.Cells.dll(我们就叫工具包吧,可以从网上下载.关于它的操作我在“Aspose.Cells操作说明 中文版 下载 Aspose C# 导出Excel 实例”一文中的 ...