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思路简述
之前几乎没写过什么这种几何的计算题.在众多大佬的博客下终于记起来了当时的公式.嘚赶快补计算几何和概率论的坑了... 这题的要求,在对两圆相交的板子略做修改后,很容易实现.这里直接给出代码.重点的部分有 ...
随机推荐
- Python 数据分析:让你像写 Sql 语句一样,使用 Pandas 做数据分析
Python 数据分析:让你像写 Sql 语句一样,使用 Pandas 做数据分析 一.加载数据 import pandas as pd import numpy as np url = ('http ...
- POJ 3255 Roadblocks (次短路)
题意:给定一个图,求一条1-n的次短路. 析:次短路就是最短路再长一点呗,我们可以和求最短路一样,再多维护一个数组,来记录次短路. 代码如下: #pragma comment(linker, &quo ...
- EditText 设置可以输入的字符,过滤不符合接口要求的数据的方法
1.设置EditText的android:digits 属性, 这种方式可以指出要支持的字符.比如要限制只能输入数字和字母,可以这样android:digits="1234567890ABC ...
- unity update优化
http://forum.china.unity3d.com/thread-13968-1-1.html Unity有个消息系统,它可以在运行中当发生指定事件时调用你在脚本中定义的那些魔术方法.这是个 ...
- django视图 CBV 和 FBV
目录 视图 CBV 和 FBV 什么是视图? FBV function based view 基于函数的视图 CBV class based view 基于类的视图 小技巧 CBV 如何获取页面请求类 ...
- ThinkSNS+ 是如何计算字符显示长度的
什么是ThinkSNS+ ThinkSNS(简称TS),一款全平台综合性社交系统,目前最新版本为ThinkSNS+.ThinkSNS V4 ThinkSNS[简]. 今天我们来聊一下可能很多人都会头疼 ...
- JS高级学习历程-17
[正则案例] 1 匹配手机号码
- Jmeter (二十六)逻辑控制器 之 Module Controller and Include Controller
Module Controller ---模块控制器 测试计划设置“独立运行没每个线程组” 线程组2中使用Module Controller执行线程组1中的Sampler: 紧接着,将线程组1disa ...
- SpringBoot用户CRUD
1.准备 http://start.spring.io/ 这里地址去直接生成你需要的项目信息,如何你本身ide以及集成了springboot 那么可以直接生成项目信息 需要的知识:java,sprin ...
- 2017 ACM/ICPC Asia Regional Shenyang Online cable cable cable
Problem Description Connecting the display screen and signal sources which produce different color s ...