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思路简述
之前几乎没写过什么这种几何的计算题.在众多大佬的博客下终于记起来了当时的公式.嘚赶快补计算几何和概率论的坑了... 这题的要求,在对两圆相交的板子略做修改后,很容易实现.这里直接给出代码.重点的部分有 ...
随机推荐
- IE11浏览器中的My97日历控件刷新后无法打开问题解决办法
IE11浏览器中的My97日历控件刷新后无法打开问题解决办法 IE11浏览器中的My97日历控件刷新后无法打开问题解决办法:(谷歌浏览器下正常.IE11失效) 解决办法:1:找到WdatePick ...
- HDU - 2689 Sort it与2016蓝桥杯B 交换瓶子 排序(相邻交换与任意交换)
Sort it You want to processe a sequence of n distinct integers by swapping two adjacent sequence ele ...
- .net 扩展方法,lamada表达式 委托
扩展方法 (1)扩展方法是一种特殊的静态方法,它定义在一个静态类中,但可以在其他类的对象上向调用实例方法那样进行调用.因此,通过扩展方法,我们就可以在不修改一个类型的前提下对一个类型进行功能上的扩充, ...
- [Xcode 实际操作]七、文件与数据-(11)数据持久化存储框架CoreData的使用:创建CoreData实体并插入数据
目录:[Swift]Xcode实际操作 本文将演示[CoreData]数据持久化存储框架的使用. 点击[Create a new Xcode project]创建一个新的项目 ->[Single ...
- transition动画最简使用方式
HTML <a href="#" title="">test</a> CSS a {display:block; width:200px ...
- 2017ACM/ICPC广西邀请赛 A Math Problem
解法:发现..最多15个,那么..暴力一下啦 #include <iostream> #include <stdio.h> #include <vector> #i ...
- 查询索引range失效
在某一个时间字段加索引,短的时间范围内查询,索引生效,为range.长时间范围,索引失效,查全表. 当索引查的数据量超过全表30%的数据,索引失效,会查全表.
- Mac OS X
Mac OS X 除了微软自家的Windows平台, .NET Core针对Mac OS以及各种Linux(RHEL.Ubuntu.Debian.Fedora.CentOS和SUSE等)都提供了很好的 ...
- (转)nginx应用总结(1)--基础认识和应用参数优化配置
在linux系统下使用nginx作为web应用服务,用来提升网站访问速度的经验已五年多了,今天在此对nginx的使用做一简单总结. 一.nginx服务简介Nginx是一个高性能的HTTP和反向代理服务 ...
- Linq 根据list属性去重复
s.Where((x, i) => s.FindIndex(z => z.ArticleTitle == x.ArticleTitle) == i).ToList();