codeforce Gym 100685F Flood (topo排序)
如果直接模拟水向周围流会TLE,因为某些个结点被重复扩展了多次,
科学做法是topo排序,每次只把入度为0的点放入队列,这样就严格保证了每个结点只被扩展一次。
#include<bits/stdc++.h>
using namespace std;
#define eps 1e-9
#define bug(x) cout<<#x<<'='<<x<<endl;
#define Min 1e-7
const int maxn = 1e4+;
const int maxe = 1e5+; struct Ponds
{
double cap,now;
}; typedef Ponds Pants; Pants P[maxn];
int N,K; vector<int> son[maxn]; int St,Tar,Amount;
int in[maxn];
queue<int> q; void topo()
{ P[St].now += Amount;
q.push(St);
for(int i = ; i <= N; i++) if(!in[i] && i!= St) q.push(i); while(q.size()){
int u = q.front(); q.pop();
Pants &p = P[u];
if(p.now - p.cap > ){
if(son[u].size()){
double each = (p.now-p.cap)/son[u].size();
p.now = p.cap;
for(int i = ; i < son[u].size(); i++){
int v = son[u][i];
P[v].now += each;
if(--in[v] == ) q.push(v);
}
}else {
p.now = p.cap;
}
} } }
int main()
{
//freopen("in.txt","r",stdin);
scanf("%d%d",&N,&K);
for(int i = ; i <= N; i++){
scanf("%lf%lf",&P[i].cap,&P[i].now);
}
for(int i = ; i < K; i++){
int u,v; scanf("%d%d",&u,&v);
son[u].push_back(v);
in[v]++;
} scanf("%d%d%d",&St,&Amount,&Tar); topo();
printf("%lf",P[Tar].now);
return ;
}
codeforce Gym 100685F Flood (topo排序)的更多相关文章
- Gym - 100685F Flood BFS
Gym - 100685F 题意:n个水池之间流水,溢出多少流出多少,多个流出通道的话平均分配,给你每个水池中的水量和容量,问到最后目标水池中水量. 思路:直接用队列扩展,不过这里有一个优化,就是统计 ...
- poj2762 缩点+topo排序
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16486 ...
- topo排序 + 用邻接表优化后的
输入数据: 4 61 21 32 33 42 44 2 4 61 21 32 33 42 41 2 topo排序为偏序: #include<stdio.h> #include<que ...
- hdu-5695 Gym Class(贪心+拓扑排序)
题目链接: Gym Class Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- hdu.5195.DZY Loves Topological Sorting(topo排序 && 贪心)
DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 ...
- Codeforce Gym 100015I Identity Checker 暴力
Identity Checker 题目连接: http://codeforces.com/gym/100015/attachments Description You likely have seen ...
- Gym Class(拓扑排序)
Gym Class Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- 2016 百度之星初赛 Gym Class(优先队列+拓扑排序)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Pract ...
- codeforce gym/100495/problem/K—Wolf and sheep 两圆求相交面积 与 gym/100495/problem/E—Simple sequence思路简述
之前几乎没写过什么这种几何的计算题.在众多大佬的博客下终于记起来了当时的公式.嘚赶快补计算几何和概率论的坑了... 这题的要求,在对两圆相交的板子略做修改后,很容易实现.这里直接给出代码.重点的部分有 ...
随机推荐
- HDU - 5094 Maze(状压+bfs)
Maze This story happened on the background of Star Trek. Spock, the deputy captain of Starship Enter ...
- Java线程安全与多线程开发
互联网上充斥着对Java多线程编程的介绍,每篇文章都从不同的角度介绍并总结了该领域的内容.但大部分文章都没有说明多线程的实现本质,没能让开发者真正“过瘾”. 从Java的线程安全鼻祖内置锁介绍开始,让 ...
- 数据库路由中间件MyCat - 源代码篇(11)
此文已由作者张镐薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 4.配置模块 每个MyCatServer初始化时,会初始化: MyCatServer.java: publi ...
- IOC模式及Unity框架文章收藏
1.IoC模式:http://www.cnblogs.com/qqlin/archive/2012/10/09/2707075.html 通过Unity实现IOC容器. 2.深入理解DIP.IoC.D ...
- AI决策算法 之 GOAP (一)
http://blog.csdn.net/lovethrain/article/details/67632033 本系列文章内容部分参考自:http://gamerboom.com/archives/ ...
- 51nod1100(计算斜率)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1100 题意:中文题啦- 思路:算斜率不用多说吧?本题唯一一个 ...
- 51nod 1831 小C的游戏
小C和小L是好朋友,她们在玩一个游戏. 一开始有一个大小为n的石子堆,小C先手. 每次可以对这个石子堆拿走一个或者把这个石子堆分成等量的几份并只取其中一份(不能不变或只剩下一个). 如果取走最后一个人 ...
- 静态文件与APP
目录 静态文件的配置和使用 什么是静态文件? 为什么使用静态文件 如何配置,使用静态文件 静态文件相关(动态配置) app创建预注册 app指什么? 创建一个APP 注册app app文件作用 app ...
- VS2015 调试出现无法启动iis express web服务器
VS2015 调试出现无法启动iis express web服务器 在项目目录下找到.vs文件夹,然后在.vs/config/applicationhost.config找到这个配置文件,删除掉,然后 ...
- luogu P2709 小B的询问 最简单的莫队
块内按右端点sort,块外按左端点sort 话说我刚开始这么修改... inline )*(c[a[i]]-),--c[a[i]];} inline )*(c[a[i]]+),++c[a[i]];} ...