First in multiple schools: Maximum Minimum Cost of HDU 4862

Source: Internet
Author: User

Idea: This question mainly refers to the creation of graphs for a long time. It was not judged to be full before, so I read this knowledge point again, then we found that there was a bug when the maximum flow of our minimum fee was full, and it was changed.

Figure: start to see the question to know that this question is the largest flow of the minimum cost, and then do it without looking at the explanation. Then I couldn't find the answer to my self-built graph, and I thought for a long time that I didn't find anything wrong. Then I read the official question and found that my graph creation is too bad for the official team. It may be caused by the creation of the minimum cost for poj yesterday. The official website is too qualified for the question. I can only say that I still understand it after reading the graph for a long time.

Construct a two-part graph. X has n * m nodes, and The Source Vertex connects an edge to each node in X. The traffic is 1 and the cost is 0. Y has n * m nodes, each node connects one edge to the sink. The traffic is 1 and the fee is 0; if node X in Part X can reach node y in part y within one step (that is, the node Y in the question can be right or down), then even the side X-> y, the cost is the energy consumed from the X-grid to the Y-grid minus the obtained energy (the maximum cost is the maximum flow, so it is a negative number), and the traffic is 1; add a new node in Part X to indicate that it can be set off from any node K times (I have understood it for a long time, for example, the third example, points 1 and 2 can only exit one step at a time, so the new point is created for the third case.) The source point is connected to the edge. The cost is 0, and the traffic is K, this vertex is connected to each vertex in the Y part. The fee is 0, the traffic is 1, and the longest flow is run in this graph. If the full flow exists, the solution does not exist, the opposite of the minimum cost is the maximum energy available.

Code explanation: B [I] [J] * 2-1 indicates that the vertex after each point is split belongs to the left graph, and B [I] [J] * 2 indicates the right graph.

# Pragma comment (linker, "/Stack: 1024000000,1024000000 ") # include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <map> # include <queue> # include <set> # include <cmath> # include <bitset> # define MEM (, b) memset (a, B, sizeof (A) # define lson I <1, L, mid # define rson I <1 | 1, Mid + 1, R # define llson j <1, L, mid # define rrson j <1 | 1, Mid + 1, R # define INF 0x7ffffffftypedef long ll; typedef unsigned long Long ull; using namespace STD; # define maxn 20005 struct {int V, W, C, next, RE; // re records the subscript of the inverse edge, C is the cost, W is traffic} e [maxn]; int sink, CNT, flow, minflow; int head [maxn], que [maxn * 10], pre [maxn], dis [maxn]; bool vis [maxn]; void add (int u, int V, int W, int c) {e [CNT]. V = V, E [CNT]. W = W, E [CNT]. C = C; E [CNT]. next = head [u]; E [CNT]. re = CNT + 1, head [u] = CNT ++; E [CNT]. V = u, E [CNT]. W = 0, E [CNT]. C =-C; E [CNT]. next = head [v]; E [CNT]. re = cnt-1, head [v] = C NT ++;} bool spfa () {int I, L = 0, r = 1; for (I = 0; I <= sink; I ++) dis [I] = inf, vis [I] = false; DIS [0] = 0, que [0] = 0, minflow = inf, vis [0] = true; while (L <r) {int u = que [L ++]; for (I = head [u]; I! =-1; I = E [I]. next) {int v = E [I]. v; If (E [I]. W & dis [v]> dis [u] + E [I]. c) {dis [v] = dis [u] + E [I]. c; minflow = min (minflow, E [I]. w); Pre [v] = I; If (! Vis [v]) {vis [v] = true; que [R ++] = V ;}} vis [u] = false;} return dis [sink]! = Inf;} int change () {int I, P; for (I = sink; I! = 0; I = E [E [p]. re]. v) {P = pre [I]; E [p]. w-= minflow; E [E [p]. re]. W + = minflow;} flow + = minflow; return minflow * Dis [sink];} int EK () {int sum = 0; while (spfa ()) sum + = change (); Return sum;} void Init () {MEM (Head,-1), Mem (PRE, 0), CNT = 0, flow = 0 ;} int main () {// freopen ("1.txt"," r ", stdin); int T, II = 1; scanf (" % d ", & T ); while (t --) {Init (); int n, m, K, I, J, K, Z, TMP = 0, B [10] [11]; char s [10] [11]; scanf ("% d", & N, & M, & K); for (I = 0; I <n; I ++) scanf ("% s", s [I]); for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) B [I] [J] = ++ TMP; for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) {for (k = J + 1; k <m; k ++) // returns to the right {z = 0; if (s [I] [J] = s [I] [k]) z = s [I] [J]-'0 '; add (B [I] [J] * 2-1, B [I] [k] *, k-j-1-z);} For (k = I + 1; k <N; k ++) // go down {z = 0; If (s [I] [J] = s [k] [J]) z = s [I] [J]-'0'; add (B [I] [J] * 2-1, B [k] [J] * 2, 1, k-i-1-z);} int new = TMP * 2 + 1; add (0, new, K, 0); // source and left New node connected for (I = 0; I <N; I ++) for (j = 0; j <m; j ++) {Add (0, B [I] [J] * 2-1, 0 ); // Add (new, B [I] [J] *, 0) is connected to each other on the left ); // connection between the new node and each node on the right} sink = TMP * 2 + 2; for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) // Add (B [I] [J] * 2, sink, 1, 0) is connected to each vertex on the right ); int ans = EK (); printf ("case % d:", II ++); If (flow! = N * m) puts ("-1"); else printf ("% d \ n",-ans) ;}return 0 ;}


First in multiple schools: Maximum Minimum Cost of HDU 4862

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.