Codeforces gym 100685 F. Flood bfs

來源:互聯網
上載者:User

標籤:

F. Flood
Time Limit: 20 Sec

Memory Limit: 256 MB

題目串連

http://codeforces.com/gym/100685/problem/F

Description

We all know that King Triton doesn‘t like us and therefore shipwrecks, hurricanes and tsunami do happen. But being bored with the same routine all these years Triton has decided to make a spectacular flood this year.

He chose a small town in a hilly valley not far from the sea. The power of Triton is enough to pour one heavy rain on the hill. He is worried however that water will miss that chosen town due to various river basins and water flows. Triton asks you to help him help calculate the amount of water that reaches the chosen town.

There are water ponds in a hilly valley on the way to the town. Some of them are connected to each other with rivers. If some pond is overfull with water, the water begins to flow evenly to the connected ponds (or to the sea, if there are no connected ponds). Each pond contains some water initially, and the maximum pond capacity is also known. The chosen town is located on the bank of one pond — you should calculate the water level in this pond after all water flow is run out.

Input

On the first line of input integers N and K (2 ≤ N ≤ 104, 0 ≤ K ≤ 105) are given — the number of water ponds and the number of pond connections respectively.

On the next N lines of input integers Pi and Ai (0 ≤ Ai ≤ Pi ≤ 106) are given — these are the maximum ith water pond capacity and its initial water level.

On the next K lines of input integers Fj and Tj (1 ≤ Fj, Tj ≤ N, Fj ≠ Tj) are given — they denote a possible river flow connection from Fj to Tj water pond (reverse water flow is not possible). Consider water flow from a pond to be equally distributed between all possible flow connections from that pond. Triton is absolutely sure that there are no cycles in river flows between the ponds, and there are no multiple rivers between any two ponds.

On the last line of input integers X, Y and Z (1 ≤ X, Z ≤ N, 1 ≤ Y ≤ 106) are given — the water pond that receives Triton‘s heavy rain, the amount of water that is added to this pond and the target pond (near the chosen town) to test respectively.

Consider that excessive water flows from a water pond if and only if its capacity is full. If some pond is overfull and no water flows are defined from that pond consider that all excessive water has flown out to the sea.

 

Output

The first line of the output should contain a single floating-point number Lz — the final water level in the target pond when all water flow is complete. Answers with absolute or relative error less than 10 - 4 are considered correct.

Sample Input

4 4
10 10
1 0
1 0
10 0
1 2
1 3
2 4
3 4
1 5 4

Sample Output

3.0

HINT

 

題意

給一個有向無環無重邊的圖,然後從一個地方倒水,水滿了就會溢出,溢出給所有相鄰的點,都平均給,然後問你最後有多少水

題解:

直接bfs就好了

代碼

 

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <vector>#include <stack>#include <map>#include <set>#include <queue>#include <iomanip>#include <string>#include <ctime>#include <list>typedef unsigned char byte;#define pb push_back#define input_fast std::ios::sync_with_stdio(false);std::cin.tie(0)#define local freopen("in.txt","r",stdin)#define pi acos(-1)using namespace std;typedef pair<double , double > dl;const int maxn = 1e4 + 50;vector<int>e[maxn];int n , k , z ;int dis[maxn];char inq[maxn];dl A[maxn];queue<int>q;double ans;double dig[maxn];void bfs(){    while(!q.empty())    {        int cur = q.front();q.pop();        inq[cur] = 0;        if (cur == z) return;        int degree = e[cur].size();        double add = (A[cur].first - A[cur].second)/((double)degree);        A[cur].first = A[cur].second;        for(int i = 0 ; i < degree ; ++ i)        {            int v = e[cur][i];            A[v].first += add;            if (A[v].first > A[v].second && !inq[v])            {                q.push(v);                inq[v] = 1;            }        }    }}int main(int argc,char *argv[]){  scanf("%d%d",&n,&k);  for(int i = 1 ; i <= n ; ++ i) scanf("%lf%lf",&A[i].second,&A[i].first);  while(k--)  {      int u , v ;      scanf("%d%d",&u,&v);      e[u].pb(v);  }  int x,y;  scanf("%d%d%d",&x,&y,&z);  A[x].first += (double)y;  memset(inq,0,sizeof(inq));  if (A[x].first > A[x].second) q.push(x);  bfs();  if (A[z].first > A[z].second ) ans = A[z].second;  else ans = A[z].first;  printf("%.8lf\n",ans);  return 0;}

 

Codeforces gym 100685 F. Flood bfs

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.