#H2026H. Heal the Network
Heal the Network
Background
In year 3077 of the Stellar Calendar, the city Astra, floating above a sea of clouds, is kept running by a vast ether energy network.
The network consists of energy nodes and one-way energy transmission pipes. Energy starts from the main reactor and is ultimately delivered to the city core . Each pipe has its own capacity limit.
The central control system has already computed a set of maximum-energy transmission plans. To avoid energy circulating pointlessly in the network, all pipes carrying positive flow in this plan do not form a directed cycle.
However, an unexpected meteor shower damaged one of the pipes, reducing its capacity by exactly .
The city core may shut down at any moment due to insufficient energy. You need to compute a new maximum flow after the pipe is damaged as fast as possible.
Description
Given a directed graph
where:
- , with nodes numbered ;
- , with edges numbered in input order;
- is the source;
- is the sink;
- The -th edge goes from to and has positive integer capacity .
Also given is an integral flow , where the -th edge currently carries units of flow.
It is guaranteed that is a maximum - flow in the original graph; that is, it satisfies:
-
,
-
For every , we have
$$\sum_{(u,v)\in E}f_{(u,v)}=\sum_{(v,w)\in E}f_{(v,w)}. $$
In addition, the given flow is acyclic. That is, after keeping only the edges with , the resulting directed graph contains no directed cycle.
Now, the capacity of the -th edge is reduced by exactly . The modified capacities are
$$c'_i= \begin{cases} c_i-1,&i=p,\\ c_i,&i\ne p. \end{cases} $$Output any integral maximum flow in the modified network.
Input Format
The first line contains five integers: , denoting the number of nodes, the number of edges, the source, the sink, and the index of the edge whose capacity is reduced, respectively.
The next lines each contain four integers: , meaning that the -th edge goes from node to node , its original capacity is , and the original maximum flow sends units of flow along this edge.
Output Format
This problem uses a Special Judge.
The first line outputs an integer , the maximum flow value in the modified network.
The second line outputs integers , where denotes the flow on the -th edge in the modified maximum flow.
The flow you output must satisfy:
- All are integers;
- for every edge;
- Flow conservation holds at every node except and ;
- The value of this flow equals ;
- is the maximum flow value of the modified network.
If multiple valid answers exist, output any one of them.
The output new maximum flow is also required to be an acyclic flow.
Constraints
For all test data:
It is guaranteed that:
- ;
- The input graph may contain parallel edges;
- The given flow is an integral maximum flow of the original graph;
- The directed graph formed by all edges with contains no directed cycle;
- The capacity of the -th edge may become after the reduction;
- The maximum flow value after the modification does not exceed .
Sample Input
5 6 1 5 4
1 2 2 2
1 3 2 1
2 3 1 0
2 4 2 2
3 4 2 1
4 5 3 3
Sample Output
3
2 1 1 1 2 3
Explanation
The value of the original maximum flow is .
The -th edge is
and its capacity was reduced from to . Therefore, of the two units of flow originally passing through this edge, one unit can no longer be delivered along the original route.
This one unit of flow can be rerouted as
The adjusted edge flows are:
| Edge ID | Pipe | New Capacity | New Flow |
|---|---|---|---|
Thus, even with one pipe damaged, the network can still maintain a maximum flow with value .
相关
在下列比赛中: