HDU 4862 jump (Multi-school joint training 1) (minimum cost, maximum flow)

Source: Internet
Author: User

Address: hdu4862

The minimum cost flow is still too small. You cannot figure it out...

Directly reference the official question...

The minimum K path coverage model is solved by the cost flow or KM algorithm. A two-part graph is constructed. X has n * m nodes, and The Source Vertex connects one edge to each node in X, with 1 traffic, the cost is 0. Department y has n * m nodes, and each node connects one edge to the sink. The traffic is 1 and the cost is 0, if node X in Part X can reach node y in part y within one step, even side X-> y, the cost is the amount of energy consumed from the X-grid to the Y-grid minus the amount of energy obtained. The traffic is 1, and a new node is added to the X-grid, which means that K requests can be initiated from any node, the Source Vertex is connected to its edge. The cost is 0, and the traffic is K. This vertex is connected to each edge in the Y part. The cost is 0, and the traffic is 1. This graph is used to run the maximum flow with the minimum cost, if a full stream is used, the solution exists; otherwise, the reverse Number of the minimum fee is the maximum energy that can be obtained.

The Code is as follows:

# Include <iostream> # include <stdio. h> # include <string. h> # include <stdlib. h> # include <math. h> # include <ctype. h >#include <queue >#include <map> # include <algorithm> using namespace STD; const int INF = 0x3f3f3f3f; int head [500], source, sink, CNT, flow, cost; int cur [500], d [500], vis [500], MP [20] [20]; struct node {int U, V, Cap, cost, next;} edge [1000000]; void add (int u, int V, int cap, int cost) {edge [CNT]. V = V; edge [CNT]. CAP = CAP; edge [CNT]. cost = cost; edge [CNT]. next = head [u]; head [u] = CNT ++; edge [CNT]. V = u; edge [CNT]. CAP = 0; edge [CNT]. cost =-cost; edge [CNT]. next = head [v]; head [v] = CNT ++;} int spfa () {memset (D, INF, sizeof (d); memset (VIS, 0, sizeof (VIS); queue <int> q; q. push (source); D [Source] = 0; cur [Source] =-1; int minflow = inf, I; while (! Q. Empty () {int u = Q. Front (); q. Pop (); vis [u] = 0; for (I = head [u]; I! =-1; I = edge [I]. next) {int v = edge [I]. v; If (d [v]> d [u] + edge [I]. cost & edge [I]. CAP) {d [v] = d [u] + edge [I]. cost; minflow = min (minflow, edge [I]. CAP); cur [v] = I; If (! Vis [v]) {vis [v] = 1; q. push (v) ;}}}if (d [sink] = inf) return 0; flow + = minflow; Cost-= minflow * d [sink]; for (I = cur [sink]; I! =-1; I = cur [edge [I ^ 1]. v]) {edge [I]. cap-= minflow; edge [I ^ 1]. cap + = minflow;} return 1;} void mcmf (INT sum) {While (spfa (); If (flow = sum) printf ("% d \ n ", cost); else printf ("-1 \ n");} int main () {int t, n, m, I, J, K, H, num = 0; char s [30]; scanf ("% d", & T); While (t --) {num ++; scanf ("% d ", & N, & M, & K); for (I = 0; I <n; I ++) {scanf ("% s", S ); for (j = 0; j <m; j ++) {MP [I] [J] = s [J]-'0' ;}} source = 0; sink = 2 * n * m + 2; CNT = 0; memset (Head,-1, sizeof (head); flow = 0; cost = 0; for (I = 1; I <= N * m; I ++) {Add (source, I, 1, 0); add (I + N * m + 1, sink, 1, 0); add (N * m + 1, I + N * m + 1, 1, 0);} Add (source, N * m + 1, K, 0 ); int Z; for (I = 0; I <n; I ++) {for (j = 0; j <m; j ++) {for (H = J + 1; H <m; H ++) {z = 0; if (MP [I] [J] = MP [I] [H]) z = MP [I] [J]; add (I * m + J + 1, N * m + 1 + I * m + H + 1, 1, h-j-1-z);} For (H = I + 1; H <n; H ++) {int z = 0; if (MP [I] [J] = MP [H] [J]) z = MP [I] [J]; add (I * m + J + 1, N * m + 1 + H * m + J +, h-i-1-z); }}printf ("case % d:", num); mcmf (N * m );} return 0 ;}


HDU 4862 jump (Multi-school joint training 1) (minimum cost, maximum flow)

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.