紫皮书的例题

照着敲了一遍,非原创

大题思路主要是三杯水,而水的总数是知道的,相当于知道第一第二杯水的体积,第三杯水的体积也就确定了。

用第一第二杯水的体积来标记数组是否遍历过

优先队列来找移动体积最少的

主要update_ans()函数进行每次判断

void update_ans(Node & u){
for(int i = ;i < ;i++){
int d = u.v[i];
if(ans[d] < || ans[d] > u.dist)
ans[d] = u.dist;
}
}

然后就是找第i杯水应该往第j杯水移动水的体积

符合条件进行入队操作

最后在ans数组寻找合适的解……!!!

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <stack>
#include <cctype>
#include <string>
#include <malloc.h>
#include <queue>
#include <map> using namespace std; const int INF = 0xffffff;
const double esp = 10e-;
const double Pi = * atan(1.0);
const int Maxn = +;
const int mod = ;
const int dr[] = {,,-,,-,,-,};
const int dc[] = {,,,-,,-,-,}; struct Node{
int v[];
int dist;
bool operator < (const Node & rhs)const{
return dist > rhs.dist;
}
}; int vis[Maxn][Maxn],cap[],ans[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 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>q; Node start;
start.dist = ;
start.v[] = ;
start.v[] = ;
start.v[] = c;
q.push(start); vis[][] = ;
while(!q.empty()){
Node u = q.top();
q.pop();
update_ans(u);
if(ans[d] >= )
break;
for(int i = ;i < ;i++){
for(int j = ;j < ;j++){
if(i == j){
continue;
}
if(u.v[i] == || u.v[j] == cap[j])
continue;
int amount = min(cap[j],u.v[i]+u.v[j]) - u.v[j];///计算将i倒入j的数量
Node u2;
memcpy(&u2,&u,sizeof(u));
u2.dist = u.dist + amount;
u2.v[i] -= amount;
u2.v[j] += amount;
if(!vis[u2.v[]][u2.v[]]){
vis[ u2.v[] ][u2.v[] ] = ;
q.push(u2);
}
}
}
}
while(d >= ){
if(ans[d] >= ){
printf("%d %d\n",ans[d],d);
return;
}
d--;
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("inpt.txt","r",stdin);
#endif
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 ;
}

uva 10603的更多相关文章

  1. UVA 10603 Fill(正确代码尽管非常搓,网上很多代码都不能AC)

    题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=1544">click here~ ...

  2. UVA 10603 - Fill BFS~

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&c ...

  3. UVa 10603 Fill [暴力枚举、路径搜索]

    10603 Fill There are three jugs with a volume of a, b and c liters. (a, b, and c are positive intege ...

  4. 【路径寻找问题】UVa 10603 - Fill

    如家大神书上的例题.第一次接触也是按代码敲得.敲的过程感觉很直观.但自己写估计会写的乱七八糟.以后不能砍得难就不愿意做这种题.否则只能做一些水题了.(PS:48) 紫书 #include<ios ...

  5. UVA 10603 Fill

    题意: 题目的意思是倒水,给出的四个数据是第一个水杯,第二个水杯,第三个水杯,和目标水量.一开始只有第三个水杯是满的,剩下的水杯是空的.倒水的时候只能把倒水出来的这个杯子倒空,或是倒水进去的杯子倒满. ...

  6. 倒水问题(Fill,UVA 10603) lrj白书 p202

    看着lrj的代码自己敲了一遍,还没调试成功.... 有时间再进行完善 /* 状态start到各个状态u1,u2,u3..... 的倒水量分别为u1.dist,u2.dist,u3.dist.... * ...

  7. 倒水问题(Fill, UVa 10603)

    [题目描述] 有三个没有刻度的水壶,容量分别为a,b和c(单位为升,都是<=200的正整数).初始时前两个水壶是空的,而第三个装满了水.每次可以从一个水壶往一个水壶里倒水,直到一个水壶倒空或者另 ...

  8. UVa 10603 倒水问题

    https://vjudge.net/problem/UVA-10603 题意:三个杯子,倒水问题.找出最少倒水量. 思路:路径寻找问题.不难,暴力枚举. #include<iostream&g ...

  9. UVa 10603 Fill (暴力BFS+优先队列)

    题意:给定4个数,a,b,c,d,分别代表空杯子容积为a,b,一个盛满水的杯子容积为c,让你不断倒水,找一个dd,是不是存在某个时刻, 某个杯子里的水dd,和d相同,或者无限接近.让求最少的倒水量和d ...

随机推荐

  1. QML在XP等显卡明显不好的情况下 可以参考

    http://doc.qt.io/qt-5/windows-requirements.html

  2. Python 数据处理扩展包: numpy 和 pandas 模块介绍

    一.numpy模块 NumPy(Numeric Python)模块是Python的一种开源的数值计算扩展.这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list str ...

  3. iOS swift lazy loading

    Why bother lazy loading and purging pages, you ask? Well, in this example, it won't matter too much ...

  4. 利用JNI技术在Android中调用C++形式的OpenGL ES 2.0函数

    1.                 打开Eclipse,File-->New-->Project…-->Android-->AndroidApplication Projec ...

  5. word2013 无endnote选项卡咋办

    word2013 无endnote选项卡咋办? 前提: 已经安装了endnotex7,office word2013 word->文件->选项->加载项->最下面的 管理 &q ...

  6. ecosphere是什么意思_ecosphere的翻译_音标_读音_用法_例句 - 必应 Bing 词典

    ecosphere是什么意思_ecosphere的翻译_音标_读音_用法_例句 - 必应 Bing 词典 ecosphere

  7. 重设mysql数据库root用户密码

     原文:http://blog.sina.com.cn/s/blog_a3695da601010mrs.html   1, 启用任务管理器,结束mysql进程   2,进入命令行,进入mysql的bi ...

  8. 使用ACE获取主机的IP地址

    使用ACE获取主机的IP地址,不知道为什么会有127.0.0.1? #include "stdafx.h" #include "ace\OS.h" #inclu ...

  9. 使用struts2和poi导出excel文档

    poi眼下应该是比較流行的操作excel的工具了.这几天做了个struts2和poi结合使用来实现导出excel的功能.个人认为还是比較有用的.代码阅读起来也非常easy.下来就来分享下我的心得 1  ...

  10. 【j2ee】div浮动层拖拽

    背景:近期项目中需要实现弹出浮层增加数据,并且浮动层可以拖拽 解决步骤:1.浮动层实现  2.拖拽实现 多方查资料,基本实现功能,现做demo,便于以后使用 先上图片大体展示实现效果: 再上代码,展示 ...