题意:n个水池之间流水,溢出多少流出多少,多个流出通道的话平均分配,给你每个水池中的水量和容量,问到最后目标水池中水量。
思路:直接用队列扩展,不过这里有一个优化,就是统计一下每个点的入度,只有对一个点访问次数达到入度次了,再将其加入队尾,这样就保证了对每个点只操作一次,不然WA、TLE各种错
1 #pragma comment(linker, "/STACK:1000000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <fstream> 5 #include <algorithm> 6 #include <cmath> 7 #include <deque> 8 #include <vector> 9 #include <queue> 10 #include <string> 11 #include <cstring> 12 #include <map> 13 #include <stack> 14 #include <set> 15 #define LL long long 16 #define INF 0x3f3f3f3f 17 #define MAXN 100005 18 #define MAXK 100005 19 #define eps 1e-8 20 using namespace std; 21 struct Node{ 22 double m, now; 23 }; 24 Node p[MAXK]; 25 int cnt[MAXK]; 26 vector<int> G[MAXK]; 27 queue<int> Q; 28 int main() 29 { 30 #ifndef ONLINE_JUDGE 31 freopen("in.txt", "r", stdin); 32 //freopen("out.txt", "w", stdout); 33 #endif // OPEN_FILE 34 int n, k; 35 scanf("%d%d", &n, &k); 36 for(int i = 1; i <= n; i++){ 37 scanf("%lf%lf", &p[i].m, &p[i].now); 38 //p[i].pos = i; 39 } 40 int x, y, z; 41 //double y; 42 memset(cnt, 0, sizeof(cnt)); 43 for(int i = 1; i <= k; i++){ 44 scanf("%d%d", &x, &z); 45 G[x].push_back(z); 46 cnt[z]++; 47 } 48 scanf("%d%d%d", &x, &y, &z); 49 // int head = 1, tail = 1; 50 p[x].now += y; 51 Q.push(x); 52 //vis[x] = true; 53 //double ans = p[z].now; 54 //int cnt = 0; 55 while(!Q.empty()){ 56 //cnt++; 57 int pos = Q.front(); 58 Q.pop(); 59 Node q = p[pos]; 60 if(q.now <= q.m) continue; 61 p[pos].now = p[pos].m; 62 if(G[pos].size() == 0) continue; 63 double u = (q.now - q.m) / G[pos].size(); 64 for(int i = 0; i < G[pos].size(); i++){ 65 //if(vis[G[q.pos][i]]) continue; 66 p[G[pos][i]].now += u; 67 //tail++; 68 cnt[G[pos][i]]--; 69 if(cnt[G[pos][i]] == 0){ 70 Q.push(G[pos][i]); 71 } 72 } 73 } 74 printf("%.6lf ", p[z].now); 75 }