Codeforces Gym 100685 F. Flood BFS

Source: Internet
Author: User

F. Flood
Time Limit:20 Sec

Memory limit:256 MB

Topic Connection

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

Description

We all know this 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 have decided to make a spectacular flood this year.

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

There is water ponds in a hilly valley on the the-the-town. Some of them is 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 N o Connected ponds). Each pond contains some water initially, and the maximum pond capacity are also known. The chosen town was located on the bank of one pond-you should calculate the water level in this pond after all water flo W is run out.

Input

On the first line of input integers n and K (2≤ n ≤104, 0≤ K ≤10 5) is 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≤ AiPi ≤106) is Given-these is the maxim Um ith Water pond capacity and its initial water level.

On the nextKlines of input integers F J and T J (1≤ F J , T J N , F J T Sub class= "Lower-index" > J ) is Given -they denote a possible river flow connection from F J to T J Water Pond (Reverse water flow is not possible). Consider water flow from a pond to being equally distributed between all possible flow connections from that pond. Triton is absolutely sure that there be no cycles in river flows between the ponds, and there are no multiple rivers BETW Een any and ponds.

On the last line of input integers x, Y and z (1≤ x, z ≤ n, 1≤ Y ≤10 6) is given-the Water pond that receives Triton ' s heavy rain, the amount of water th At are 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 it capacity is full. If some pond is overfull and no water flows be defined from that pond consider so all excessive water have flown out to The sea.

Output

The first line of the output should contain a single floating-point number Lz -the final water Level of the target pond when all water flow are complete. Answers with absolute or relative error less than 4 is 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

Test instructions

Give a picture of a direction without a loop, then pour water from a place, the water will overflow, overflow to all adjacent points, average, and then ask how much water you have in the end

The following:

Direct BFS is good.

Code

#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 unsignedChar byte;#definePB Push_back#defineInput_fast Std::ios::sync_with_stdio (false); Std::cin.tie (0)#defineLocal Freopen ("In.txt", "R", stdin)#definePi ACOs (-1)using namespaceStd;typedef pair<Double,Double>DL;Const intMAXN = 1e4 + -; Vector<int>E[MAXN];intN, K, z;intDIS[MAXN];CharINQ[MAXN];d l a[maxn];queue<int>Q;Doubleans;DoubleDIG[MAXN];voidBFs () { while(!Q.empty ()) {        intCur =Q.front (); Q.pop (); Inq[cur]=0; if(cur = = z)return; intdegree =e[cur].size (); DoubleAdd = (A[cur].first-a[cur].second)/((Double) degree); A[cur].first=A[cur].second;  for(inti =0; I < degree; ++i) {intv =E[cur][i]; A[v].first+=add; if(A[v].first > A[v].second &&!)Inq[v])                {Q.push (v); INQ[V]=1; }        }    }}intMainintargcChar*argv[]) {scanf ("%d%d",&n,&k);  for(inti =1; I <= N; + + i) scanf ("%LF%LF",&a[i].second,&A[i].first);  while(k--)  {      intu, v; scanf ("%d%d",&u,&v);  E[U].PB (v); }  intx, 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; ElseAns =A[z].first; printf ("%.8lf\n", ans); return 0;}

Codeforces Gym 100685 F. Flood BFS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.