Power Network
Time limit1sMemory limit128 MB
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.
Problem
A power network consists of nodes (power stations, consumers, and dispatchers) connected by power transport lines. A node may be supplied with an amount of power, may produce an amount of power, may consume an amount of power, and may deliver an amount of power. The following restrictions apply: for any power station, for any consumer, and for any dispatcher. There is at most one power transport line from a node to a node in the network; it transports an amount of power delivered by to . Let be the total power consumed in the network. Your task is to compute the maximum possible value of .

Figure 1. A power network.
The example above illustrates one valid state of the network. The label of a power station means and . The label of a consumer means and . The label of a power transport line means and . Here the power consumed is . There are other possible states of the network, but the value of 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 , the number of power stations , the number of consumers , and the number of power transport lines .
Then follow triplets of the form (u,v)z, where and are node identifiers (numbered from 0) and is the value of .
Then follow doublets of the form (u)z, where is the identifier of a power station and is the value of .
The data set ends with doublets of the form (u)z, where is the identifier of a consumer and is the value of .
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.