HDU 5352 MZL & #39; s City (maximum minimum cost stream) Classic 2015 Multi-University Training Contest 5
MZL's CityTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission (s): 546 Accepted Submission (s): 185
Problem Description MZL is an active girl who has her own country.
Her big country has N cities numbered from 1 to N. she has controled the country for so long and she only remebered that there was a big earthquake M years ago, which made all the roads between the cities destroyed and all the city became broken. she also remebered that exactly one of the following things happened every recent M years:
1. She rebuild some cities that are connected with X directly and indirectly. Notice that if a city was rebuilt that it will never be broken again.
2. There is a bidirectional road between city X and city Y built.
3. There is a earthquake happened and some roads were destroyed.
She forgot the exactly cities that were rebuilt, but she only knew that no more than K cities were rebuilt in one year. now she only want to know the maximal number of cities that cocould be rebuilt. at the same time she want you to tell her the smallest lexicographically plan under the best answer. notice that 8 2 1 is smaller than 10 0 1.
Input The first contains one integer T (T <= 50), indicating the number of tests.
For each test, the first line contains three integers N, M, K (N <= 200, M <= 500, K <= 200 ), indicating the number of MZL's country, the years happened a big earthquake and the limit of the rebuild. next M lines, each line contains a operation, and the format is "1 x", "2 x y", or a operation of type 3.
If it's type 3, first it is a interger p, indicating the number of the destoyed roads, next 2 * p numbers, describing the p destoyed roads as (x, y ). it's guaranteed in any time there is no more than 1 road between every two cities and the road destoyed must exist in that time.
Output The First line Ans is the maximal number of the city rebuilt, the second line is a array of length of tot describing the plan you give (tot is the number of the operation of type 1 ).
Sample Input
15 6 22 1 2 2 1 31 11 23 1 1 21 2
Sample Output
30 2 1Hint No city was rebuilt in the third year,city 1 and city 3 were rebuilt in the fourth year,and city 2 was rebuilt in the sixth year.
Source 2015 Multi-University Training Contest 5: There are n cities, m years (m operations can be considered), and a maximum of k cities can be repaired each year. Operation: 1 x: indicates that the city directly or indirectly connected to x can be repaired in the current year (each city can only repair 1 time, and a maximum of k cities can be repaired each year) 2 x y: indicates that a route was built in that year. Undirected. 3 p next p path x y: indicates that p path is destroyed that year The final answer outputs two rows: the first row outputs the maximum number of cities that can be repaired. The second row outputs the number of cities repaired each year for the 1 operation (the smallest output sequence ).
Problem solving: The maximum traffic with minimum cost. First, we use a two-dimensional array to record the link state. Graph: The number of years is also regarded as a vertex year + n and edge is created with the superpolar Source Vertex vs. The edge capacity is k, and the cost is m (m is decreasing, in this way, a fee is added to find the minimum solution of the Lexicographic Order (the maximum first flow period). The edge is <vs, year + n, k, m>. If operation 1 is encountered, use bfs or dfs to locate the vertex Y connected to x, and then add some new edges <year + n, Y, 1, 0>, at last, connect the edge of each I city to the vt of the superpolar junction point <I, vt, 1, 0>, and the edge is 1 because each city can only repair once. The maximum traffic for the last run is the minimum cost, and the maximum traffic is the number of cities that can be repaired each year.
# Include
# Include
# Include
Using namespace std; const int MAXN = 1010; const int MAXM = 100100; const int INF = 1 <30; struct EDG {int to, next, cap, flow; int cost; // unit price} edg [MAXM]; int head [MAXN], eid; int pre [MAXN], cost [MAXN]; // point 0 ~ (N-1) void init () {eid = 0; memset (head,-1, sizeof (head);} void addEdg (int u, int v, int cap, int cst) {edg [eid]. to = v; edg [eid]. next = head [u]; edg [eid]. cost = cst; edg [eid]. cap = cap; edg [eid]. flow = 0; head [u] = eid ++; edg [eid]. to = u; edg [eid]. next = head [v]; edg [eid]. cost =-cst; edg [eid]. cap = 0; edg [eid]. flow = 0; head [v] = eid ++;} bool inq [MAXN]; int q [MAXN]; bool spfa (int sNode, int eNode, int n) {int l = 0, r = 0; for (int I = 0; I
0 & cost [v]> cost [u] + edg [I]. cost) {// the minimum cost of cost [v] = cost [u] + edg [I] When the stream can be increased. cost; pre [v] = I; // record the edge if (! Inq [v]) {if (r = MAXN) r = 0; q [r ++] = v; inq [v] = 1 ;}}}} return cost [eNode]! = INF; // determine whether there is a augmented path} // The reverse return is the largest stream. The minimum cost is minCostint minCost_maxFlow (int sNode, int eNode, int & minCost, int n) {int ans = 0; while (spfa (sNode, eNode, n) {int mint = INF; for (int I = pre [eNode]; I! =-1; I = pre [edg [I ^ 1]. to]) {if (mint> edg [I]. cap-edg [I]. flow) mint = edg [I]. cap-edg [I]. flow;} ans + = mint; for (int I = pre [eNode]; I! =-1; I = pre [edg [I ^ 1]. to]) {edg [I]. flow + = mint; edg [I ^ 1]. flow-= mint; minCost + = mint * edg [I]. cost ;}} return ans;} int vist [MAXN], mapt [MAXN] [MAXN], year; void bfs (int u, int n) // find the point {queue connected to the u
Q; memset (vist, 0, sizeof (vist); vist [u] = 1; q. push (u); while (! Q. empty () {u = q. front (); q. pop (); for (int I = 1; I <= n; I ++) if (! Vist [I] & mapt [u] [I]) vist [I] = 1, q. push (I) ;}for (int I = 1; I <= n; I ++) if (vist [I]) addEdg (year + n, I, 1, 0);} int main () {int n, m, k, op, u, v; int T; scanf (% d, & T); while (T --) {scanf (% d, & n, & m, & k); memset (mapt, 0, sizeof (mapt); init (); year = 0; int vs = 0, vt = n + m + 1; for (int I = 1; I <= n; I ++) addEdg (I, vt, 1, 0 ); while (m --) {scanf (% d, & op, & u); if (op = 3) {int p = u; while (p --) {scanf (% d, & u, & v); m Apt [u] [v] = mapt [v] [u] = 0 ;}} else {if (op = 2) {scanf (% d, & v ); mapt [u] [v] = mapt [v] [u] = 1;} else {year ++; addEdg (vs, year + n, k, m ); bfs (u, n) ;}} int ans, tans [505]; ans = minCost_maxFlow (vs, vt, m, vt + 1 ); for (int I = head [vs]; I! =-1; I = edg [I]. next) tans [edg [I]. to-n] = edg [I]. flow; printf (% d, ans); for (int I = 1; I <= year; I ++) if (I