Power Network

Time limit1sMemory limit128 MB

Summary
Given a network of power stations, consumers, and dispatchers with capacity limits on production, consumption, and edges, compute the maximum feasible total consumption via a max-flow reduction.
Level

Medium6 of 10

Topics
Graph, Math
Solved
No attempts yet

Problem

A power network consists of nodes (power stations, consumers, and dispatchers) connected by power transport lines. A node uu may be supplied with an amount s(u)≥0s(u) \ge 0 of power, may produce an amount 0≤p(u)≤pmax⁡(u)0 \le p(u) \le p_{\max}(u) of power, may consume an amount 0≤c(u)≤min⁡(s(u),cmax⁡(u))0 \le c(u) \le \min(s(u), c_{\max}(u)) of power, and may deliver an amount d(u)=s(u)+p(u)−c(u)d(u) = s(u) + p(u) - c(u) of power. The following restrictions apply: c(u)=0c(u) = 0 for any power station, p(u)=0p(u) = 0 for any consumer, and p(u)=c(u)=0p(u) = c(u) = 0 for any dispatcher. There is at most one power transport line (u,v)(u, v) from a node uu to a node vv in the network; it transports an amount 0≤l(u,v)≤lmax⁡(u,v)0 \le l(u, v) \le l_{\max}(u, v) of power delivered by uu to vv. Let Con=∑uc(u)Con = \sum_u c(u) be the total power consumed in the network. Your task is to compute the maximum possible value of ConCon.

utypes(u)p(u)c(u)d(u)
0power station0404
12204
3consumer4022
45014
53030
2dispatcher6006
60000

Figure 1. A power network.

The example above illustrates one valid state of the network. The label x/yx/y of a power station uu means p(u)=xp(u) = x and pmax⁡(u)=yp_{\max}(u) = y. The label x/yx/y of a consumer uu means c(u)=xc(u) = x and cmax⁡(u)=yc_{\max}(u) = y. The label x/yx/y of a power transport line (u,v)(u, v) means l(u,v)=xl(u, v) = x and lmax⁡(u,v)=yl_{\max}(u, v) = y. Here the power consumed is Con=6Con = 6. There are other possible states of the network, but the value of ConCon can never exceed 6.

Input

The input contains several data sets. Each data set encodes one power network. It begins with four integers: the number of nodes 0≤n≤1000 \le n \le 100, the number of power stations 0≤np≤n0 \le n_p \le n, the number of consumers 0≤nc≤n0 \le n_c \le n, and the number of power transport lines 0≤m≤n20 \le m \le n^2.

Then follow mm triplets of the form (u,v)z, where uu and vv are node identifiers (numbered from 0) and 0≤z≤10000 \le z \le 1000 is the value of lmax⁡(u,v)l_{\max}(u, v).

Then follow npn_p doublets of the form (u)z, where uu is the identifier of a power station and 0≤z≤100000 \le z \le 10000 is the value of pmax⁡(u)p_{\max}(u).

The data set ends with ncn_c doublets of the form (u)z, where uu is the identifier of a consumer and 0≤z≤100000 \le z \le 10000 is the value of cmax⁡(u)c_{\max}(u).

All input numbers are integers. Apart from the (u,v)z triplets and the (u)z doublets, which contain no white space, white space may appear freely in the input. The input terminates at end of file and is guaranteed to be correct.

Output

For each data set, print on a separate line the maximum amount of power that can be consumed in the corresponding network. Every result is an integer and is printed starting at the beginning of its own line.

Examples3

  1. Example 1

    Input
    2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20
    7 2 3 13 (0,0)1 (0,1)2 (0,2)5 (1,0)1 (1,2)8 (2,3)1 (2,4)7
             (3,5)2 (3,6)5 (4,2)7 (4,3)5 (4,5)1 (6,0)5
             (0)5 (1)2 (3)2 (4)1 (5)4
    
    Expected output
    15
    6
    
  2. Example 2

    Input
    0 0 0 0
    
    Expected output
    0
    
  3. Example 3

    Input
    2 1 1 1 (0,1)5 (0)10 (1)10
    
    Expected output
    5