Gym - 100685F Flood BFS
题意:n个水池之间流水,溢出多少流出多少,多个流出通道的话平均分配,给你每个水池中的水量和容量,问到最后目标水池中水量。
思路:直接用队列扩展,不过这里有一个优化,就是统计一下每个点的入度,只有对一个点访问次数达到入度次了,再将其加入队尾,这样就保证了对每个点只操作一次,不然WA、TLE各种错
#pragma comment(linker, "/STACK:1000000000")
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
#define MAXN 100005
#define MAXK 100005
#define eps 1e-8
using namespace std;
struct Node{
double m, now;
};
Node p[MAXK];
int cnt[MAXK];
vector<int> G[MAXK];
queue<int> Q;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // OPEN_FILE
int n, k;
scanf("%d%d", &n, &k);
for(int i = ; i <= n; i++){
scanf("%lf%lf", &p[i].m, &p[i].now);
//p[i].pos = i;
}
int x, y, z;
//double y;
memset(cnt, , sizeof(cnt));
for(int i = ; i <= k; i++){
scanf("%d%d", &x, &z);
G[x].push_back(z);
cnt[z]++;
}
scanf("%d%d%d", &x, &y, &z);
// int head = 1, tail = 1;
p[x].now += y;
Q.push(x);
//vis[x] = true;
//double ans = p[z].now;
//int cnt = 0;
while(!Q.empty()){
//cnt++;
int pos = Q.front();
Q.pop();
Node q = p[pos];
if(q.now <= q.m) continue;
p[pos].now = p[pos].m;
if(G[pos].size() == ) continue;
double u = (q.now - q.m) / G[pos].size();
for(int i = ; i < G[pos].size(); i++){
//if(vis[G[q.pos][i]]) continue;
p[G[pos][i]].now += u;
//tail++;
cnt[G[pos][i]]--;
if(cnt[G[pos][i]] == ){
Q.push(G[pos][i]);
}
}
}
printf("%.6lf\n", p[z].now);
}
Gym - 100685F Flood BFS的更多相关文章
- codeforce Gym 100685F Flood (topo排序)
如果直接模拟水向周围流会TLE,因为某些个结点被重复扩展了多次, 科学做法是topo排序,每次只把入度为0的点放入队列,这样就严格保证了每个结点只被扩展一次. #include<bits/std ...
- Codeforces gym 100685 F. Flood bfs
F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...
- ACM: Gym 101047E Escape from Ayutthaya - BFS
Gym 101047E Escape from Ayutthaya Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- Codeforces Gym 100187E E. Two Labyrinths bfs
E. Two Labyrinths Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/prob ...
- Gym 100952F&&2015 HIAST Collegiate Programming Contest F. Contestants Ranking【BFS+STL乱搞(map+vector)+优先队列】
F. Contestants Ranking time limit per test:1 second memory limit per test:24 megabytes input:standar ...
- Gym 101617J Treasure Map(bfs暴力)
http://codeforces.com/gym/101617/attachments 题意:给出一个图,每个顶点代表一个金矿,每个金矿有g和d两个值,g代表金矿初始的金子量,d是该金矿每天的金子量 ...
- Gym - 100971J (思维+简单bfs)
题目链接:http://codeforces.com/gym/100971/problem/J J. Robots at Warehouse time limit per test 2.0 s mem ...
- [LeetCode] 733. Flood Fill_Easy tag: BFS
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...
- Gym - 101147E E. Jumping —— bfs
题目链接:http://codeforces.com/gym/101147/problem/E 题意:当人在第i个商店时,他可以向左或向右跳di段距离到达另一个商店(在范围之内),一个商店为一段距离. ...
随机推荐
- shell的通俗理解
(引自:https://zhidao.baidu.com/question/557066905.html) [一] shell的含义: 首先shell的英文含义是“壳”: 它是相对于内核来说的,因为它 ...
- 【codeforces 257D】Sum
[题目链接]:http://codeforces.com/problemset/problem/257/D [题意] 给你n个数字; 这n个数字组成的数组满足: a[i-1]<=a[i]< ...
- MarkDown 图片大小问题
本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50099843 MarkDown里显示图 ...
- Qt 3D教程(二)初步显示3D的内容
Qt3D教程(二)初步显示3D的内容 前一篇很easy,全然就没有牵涉到3D的内容,它仅仅是我们搭建3D应用的基本框架而已,而这一篇.我们将要利用它来初步地显示3D的内容了! 本次目的是将程序中间的内 ...
- (三)ng-app的使用困惑和angularJS框架的自己主动载入
ng-app是angular的一个指令,代表一个angular应用(也叫模块).使用ng-app或ng-app=""来标记一个DOM结点.让框架会自己主动载入.也就是说,ng-ap ...
- php使用flock堵塞写入文件和非堵塞写入文件
php使用flock堵塞写入文件和非堵塞写入文件 堵塞写入代码:(全部程序会等待上次程序运行结束才会运行,30秒会超时) <?php $file = fopen("test.txt&q ...
- nj11--http
概念:Node.js提供了http模块.其中封装了一个高效的HTTP服务器和一个建议的HTTP客户端. http.server是一个基于事件的HTTP服务器.内部有C++实现.接口由JavaScrip ...
- less05 作用域
less @clolor:#ffffff; .bgcolor{ width: 50px; a{ color: @clolor; } @clolor:#ff0000; //覆盖,作用域跟js一样,现在局 ...
- spark 卡在spark context,运行出现spark Exception encountered while connecting to the server : javax.security.sasl.SaslException
原因: 使用root用户运行spark代码 解决方法:使用非管理员账户运行spark即可 [userone@localhost bin]$ ./add-user.sh What type of use ...
- [USACO09JAN] 气象测量/气象牛The Baric Bovine 解题报告(DP)
题目链接:https://www.luogu.org/problemnew/show/P2933 Description 为了研究农场的气候,Betsy帮助农夫John做了N(1 <= N &l ...