[ACM_模拟] The Willy Memorial Program (poj 1073 ,联通水管注水模拟)
Description
Poor Willy was boiled in hot water, but his memory is still in our hearts. Though Stan tried his best, we want to write a program, in the memory of Willy, to compute the time Stan had, to rescue Willy, assuming he started to run just when the doctor opened the valve.
To simplify the problem, assume the pipes are all vertical cylinders with diameter 1 cm. Every pipe is open from the top and closed at the bottom. Some of the pipes are connected through special horizontal pipes named links. The links have very high flow capacity, but are so tiny that at any given time, the volume of water inside them is negligible. The water enters from top of one of the pipes with a constant rate of 0.25PI cm3/sec and begins to fill the pipe from the bottom until the water reaches a link through which it flows horizontally and begins to fill the connected pipe. From elementary physics we know if two pipes are connected and the surface of the water is above the connecting link, the level of water in both pipes remains the same when we try to fill one of them. In this case the water fills each pipe with a rate equal to half of the rate of incoming water. As an example, consider the following configuration:
First, the lower 2 centimeters of the left pipe is filled with water at full rate, then, the lower 3 centimeters of the right pipe is filled, and after that, the upper part of the two pipes are filled in parallel at half rate. The input to your program is a configuration of pipes and links, and a target level in one of the pipes (the heavy dotted line in the above figure). The program should report how long it takes for the level of water to reach the target level. For the above configuration, the output is 9 seconds.
It is assumed that the water falls very rapidly, such that the time required for the water to fall can be neglected. The target level is always assumed to be a bit higher than the specified level for it. As an example, if we set the target point to level 4 in the left pipe in the figure above, the elapsed time for water to reach that target is assumed to be 5 (not 2), Also note that if the water reaches to the top of a pipe (say in level x), it won't pour out outside the pipe until empty spaces in connected pipes below level x are filled (if can be filled, i.e. the level of water reaches the connecting links). (Note that there may be some links at level x, to which water is entered). After all such spaces are filled; the water level would not go up further.
Input
The first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case is p (1 <= p <= 20), the number of pipes, followed by p lines, each describing a pipe. Each pipe description line consists of three numbers. The first two are (x, y) coordinates of the upper-left corner of the pipe and the third number is the height of the pipe (at least 1 cm and at most 20 cm). Note that diameter of each pipe is 1 cm.
After input data describing the pipes, there is a line containing a single integer l, which is the number of links (0 <= l <= 50). After it, there are l lines describing links. Each link description contains 3 integers. The first two are (x, y) coordinates of the left end-point of the link and the third is the length of the link (at least 1 cm and at most 20 cm). It is assumed that the width of the link is zero.
The last line for each test case contains two numbers. The first is the number of target pipe (starting from one, with the order appeared in test data). The second line is the desired y for the level of water in the target pipe (note that the specified level may be out of the pipe at all).
You can assume the following about the input:
. The water enters into the first pipe.
. No link crosses a pipe.
. No two links have the same y coordinates.
. No two pipes have the same upper-left x coordinates.
. Both endpoints of each link are connected to pipes.
Output
Sample Input
1
2
2 0 6
5 1 6
1
3 4 2
2 2
Sample Output
9
Source
题目大意:有一些竖直的圆筒状管子,管子间有理想的水平细管相连。一只蜘蛛停在某跟管子的某个高度上,从第一根管子往系统注水,问前往营救的蜘蛛有多长时间可以关闭热水阀门。最终抽象为当水上升到指定位置的时候系统中的总水柱高度。这里第一行数据表示有t种情况,接下来2表示有2个管子,第三行和第四行分别表示这2个管子的左上角坐标和管子的长度,下面的1表示有1个连接,该链接左端坐标(3,4),长为2,接下来的2,2表示蜘蛛在第2号管子的y=2处。
解题思路:从第一根管子开始注水,采用优先队列的方式每次选取可注水且纵坐标最大(最低)的地方进行注水。若中途出现溢出现象(从其他管子的上端流出),则永远都淹不到蜘蛛。
相关知识:pair 的用法 http://www.cnblogs.com/Nimeux/archive/2010/10/05/1844191.html
lower_bound的用法 http://blog.csdn.net/niushuai666/article/details/6734403
#include <iostream>
#include <cstring>
#include <string>
#include <string.h>
#include <set>
#include <list>
#include <queue>
using namespace std;
#define maxp 30 int cases,pipes,links,target,level,tot_water;
int w,p,pp;
int px[maxp],py[maxp],bottom[maxp],water[maxp];//一个管道内x,上面y,下面y,当前水位
bool find_ans;
set<int> link;//保存连接的y值
list<pair<int, int> > e[maxp];//保存管i上的连接的位置和另一端的管的编号
priority_queue<pair<int, int> > pq;//保存当前水位(y值和管道标号) void read(){
cin>>pipes;//输入管道
for(int i=;i<pipes;i++){
int tmp;
cin>>px[i]>>py[i]>>tmp;
bottom[i]=py[i]+tmp; //管i的底部y值
water[i]=-; //标记means !visit pipe i
e[i].clear();
}
cin>>links;//输入连接
link.clear();
for(int i=;i<links;i++){
int lx,ly,len,lk1=-,lk2=-;
cin>>lx>>ly>>len;//输入每个连接并计算该连接连的管道lk1,lk2
for(int j=;j<pipes;j++){
if(bottom[j]>=ly && py[j]<=ly){
if(px[j]+==lx)lk1=j;
if(px[j]==lx+len)lk2=j;
if(lk1!=- && lk2!=-)break;
}
}
link.insert(ly);
e[lk1].push_back(make_pair(ly, lk2));
e[lk2].push_back(make_pair(ly, lk1));
}
cin>>target>>level;
link.insert(level);//把蜘蛛的位置也传入连接!!!
target--;
}
bool init(){
if(level<py[target]){
cout<<"No Solution\n";
return ;
}//当蜘蛛在管道外时,无解
//如果蜘蛛位于target管道中就虚拟一个target和-2管相连的连接(-2可以起到标记作用)
if(py[target]<=level && level<=bottom[target])
e[target].push_back(make_pair(level,-));
//延长每条连接把与每个管道的交点保存成i和i管相连的连接的形式
//把每个管道的上端标记为其和-1相连的形式,并把e[i]排序颠倒
for(int i=;i<pipes;i++){
for(set<int>::const_iterator j=link.lower_bound(py[i]);
j!=link.end()&&*j<bottom[i];j++){
e[i].push_back(make_pair(*j,i));
}
e[i].push_back(make_pair(py[i],-));
e[i].sort();
e[i].reverse();
}
return ;
}
void solve(){
while (!pq.empty())pq.pop();//清空pq
water[]=bottom[];
pq.push(make_pair(water[],));//从第一个管道开始倒水
find_ans=false;
tot_water=;
while (!pq.empty()){
w=pq.top().first; //water
p=pq.top().second; //pipe
pq.pop();
if(p==-)break;//溢出
else if(p==-){//淹没
find_ans=true;
break;
}
tot_water+=(water[p]-w);//把p管流到w位置
water[p]=w;
//把当前w位置所有与p连接的处理
while(!e[p].empty() && e[p].front().first==water[p]){
pp=e[p].front().second;
if(pp<)pq.push(e[p].front());//如果不是实连接(-1,-2的情况)直接放入pq中
else if(water[pp]==-){//如果实连接且还没有水,就更新该管水位,并放入pq中
water[pp]=bottom[pp];
pq.push(make_pair(water[pp], pp));
}
e[p].pop_front();
}
if(!e[p].empty()) pq.push(make_pair(e[p].front().first, p));//如果p管还有连接,就把它放入pq中
}
}
int main(){
cin>>cases;
while (cases--){
read();
if(!init())continue;
solve();
if (find_ans)
cout<<tot_water<<'\n';
else
cout<<"No Solution\n";
}
return ;
}
[ACM_模拟] The Willy Memorial Program (poj 1073 ,联通水管注水模拟)的更多相关文章
- poj 1008:Maya Calendar(模拟题,玛雅日历转换)
Maya Calendar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 64795 Accepted: 19978 D ...
- POJ 3414 Pots【bfs模拟倒水问题】
链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...
- POJ 1068 Parencodings【水模拟--数括号】
链接: http://poj.org/problem?id=1068 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27454#probl ...
- [ACM_模拟] POJ1068 Parencodings (两种括号编码转化 规律 模拟)
Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two diff ...
- POJ 1027 The Same Game(模拟)
题目链接 题意 : 一个10×15的格子,有三种颜色的球,颜色相同且在同一片内的球叫做cluster(具体解释就是,两个球颜色相同且一个球可以通过上下左右到达另一个球,则这两个球属于同一个cluste ...
- POJ 3078 - Shuffle'm Up - [模拟题]
题目链接:http://poj.org/problem?id=3087 Description A common pastime for poker players at a poker table ...
- poj 3087 Shuffle'm Up (模拟过程)
Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuff ...
- POJ 1035 Spell checker (模拟)
题目链接 Description You, as a member of a development team for a new spell checking program, are to wri ...
- poj 2632 Crashing Robots(模拟)
链接:poj 2632 题意:在n*m的房间有num个机器,它们的坐标和方向已知,现给定一些指令及机器k运行的次数, L代表机器方向向左旋转90°,R代表机器方向向右旋转90°,F表示前进,每次前进一 ...
随机推荐
- H5测试区别与PC端测试关注点
除了基本的业务逻辑功能测试之后,H5页面的测试,需要关注以下几点: 1. 通过H5网页(非手机的返回功能)的返回功能可以返回,不会出现无法返回的情况. 2. 横屏竖屏相互切换,能自适应,并且布局不 ...
- VC++ CTreeCtrl 使用NM_CLICK和TVN_SELCHANGED
//这是当CTREECTRL控件点击时NM_CLICK的处理函数 void CDriverSelCtrl::OnNMClick(NMHDR *pNMHDR, LRESULT *pResult) { C ...
- SER SERVER存储过程
Transact-SQL中的存储过程,非常类似于C#语言中的方法,可以重复调用.当存储过程执行一次后,可以将语句存储到缓存中,这样下次执行的时候直接使用缓存中的语句.这样就可以提高存储过程的性能. 一 ...
- css属性word-spacing和letter-spacing的区别
word-spacing和letter-spacing用来定义单词或者字母之间的水平空白间隔.顾名思义,word-spacing定义了单词之间的空白,例如: <div style="w ...
- [python] import curses
python 中,我们使用 curses.wrapper 来创建终端交互window.使用 stdscr 来代表 window 对象. 使用方法: from curses import wrapper ...
- js alert重写,适用于手机端,改自于网上的代码
<!DOCTYPE html> <html> <head> <meta name="viewport" content="wid ...
- UTF-8 <==> unicode(WCHAR)
static int fetchWordFromUTF8(const chConstStringA& strText, WCHAR& result) { int nLength = s ...
- [原创] Legato 8.1 oracle full backup skip 奇怪的问题处理过程 -- 非调度日期手工运行调度也不成功(skip)
转载请注明出处: http://www.cnblogs.com/fengaix6/p/4677024.html 作者:飄ぺ風 环境: a. Server: Legato 8.1.2, aix 6.1 ...
- js关闭浏览器的tab页(兼容)
由于在脚本中使用了 window.close(), 当前非弹出窗口在最新版本的chrome和firefox里总是不能关闭,而在 IE中是可以关闭的 . 在console中弹出提示"Scrip ...
- 数据库操作(C#)
数据库在软件开发中发挥着举足轻重的作用,基本上所有的大项目都会用到数据库.ADO .Net是一组向.Net程序员公开数据访问服务的类,其主要分为数据提供程序(Data Provider)和数据集(Da ...