HDU3395 (minimum fee flow) and hdu3395 minimum fee Flow
A good question is not difficult, as long as you notice that you do not need to let the stream affect the cost when you calculate the minimum cost, the most direct way is to let the graph directly full flow, and then find the minimum cost
/*************************************** * ******** Author: xdloveCreated Time: Tuesday, August 18, 2015 54 seconds File Name: xd. cpp *************************************** * *********/# include <stdio. h> # include <string. h> # include <iostream> # include <algorithm> # include <vector> # include <queue> # include <set> # include <map> # include <string> # include <math. h> # include <stdlib. h> # include <time. h> using namespace std ;/* * ****************************** Please don't hack me !! /(ToT )/~~ __------__/~ ~ \ | // ^ \/^ \ | /~~ \ | T | :~ \ | 6 | ___ | _ |: |\__./o \/'| (O )/~~~~ \ '\/| ~~ \ | )~ ------~ '\/' | /____/~~~) \ (_/'| (| \/__) /\\\\\// '\\\\|\/ | \___ |\|\|\____/||// ^ ~> \_/ <| \\| |\\-^-\\ |) '\ _______/^ \______/******************************** * ***/# Define clr () memset (a, 0, sizeof (a); typedef long ll; const int MAXN = 220; const int MAXM = 1e4 + MAXN; const int INF = 0x3f3f3f; struct DoubleQueue {int l, r, q [MAXN]; DoubleQueue () {l = r = 0;} bool empty () {return l = r ;} void push_back (int v) {q [r ++] = v; r % = MAXN;} void push_front (int v) {l = (L-1 + MAXN) % MAXN; q [l] = v;} int front () {return q [l];} void pop_front () {l ++; l % = MAXN;} void pop_back () {r = (r-1 + MAXN) % MAXN ;}; struct Edge {int to, next, cap, flow, cost;} edge [MAXM]; int head [MAXN], tol; int pre [MAXN], dis [MAXN]; bool vis [MAXN]; int N; void init (int n) {N = n; tol = 0; memset (head,-1, sizeof (head);} void addedge (int u, int v, int cap, int cost) {cost * =-1; // printf ("% d % D % d \ n ", u, v, cap, cost); edge [tol]. to = v; edge [tol]. cap = cap; edge [tol]. cost = cost; edge [tol]. flow = 0; edge [tol]. next = head [u]; head [u] = tol ++; edge [tol]. to = u; edge [tol]. cap = 0; edge [tol]. cost =-cost; edge [tol]. flow = 0; edge [tol]. next = head [v]; head [v] = tol ++;} bool spfa (int s, int t) {DoubleQueue q; for (int I = 0; I <N; I ++) {dis [I] = INF; vis [I] = false; pre [I] =-1;} dis [s] = 0; Vis [s] = true; q. push_back (s); while (! Q. empty () {int u = q. front (); q. pop_front (); vis [u] = false; for (int I = head [u]; ~ I; I = edge [I]. next) {int v = edge [I]. to; if (edge [I]. cap> edge [I]. flow & dis [v]> dis [u] + edge [I]. cost) {dis [v] = dis [u] + edge [I]. cost; pre [v] = I; if (! Vis [v]) {vis [v] = true; if (! Q. empty () & dis [v] <= dis [q. front ()]) q. push_front (v); else q. push_back (v) ;}}}if (pre [t] =-1) return false; return true;} int Minflow (int s, int t) {int cost = 0; while (spfa (s, t) {int Min = INF; for (int I = pre [t]; ~ I; I = pre [edge [I ^ 1]. to]) {if (Min> edge [I]. cap-edge [I]. flow) Min = edge [I]. cap-edge [I]. flow;} // cout <Min <endl; for (int I = pre [t]; ~ I; I = pre [edge [I ^ 1]. to]) {edge [I]. flow + = Min; edge [I ^ 1]. flow-= Min; cost + = edge [I]. cost * Min;} return-cost;} int bit [MAXN]; char s [MAXN]; int main () {// freopen ("in.txt", "r ", stdin); // freopen ("out.txt", "w", stdout); int n; while (~ Scanf ("% d", & n) {for (int I = 1; I <= n; I ++) scanf ("% d ", & bit [I]); init (n * 2 + 2); int ss = 0, tt = n * 2 + 1; for (int I = 1; I <= n; I ++) {scanf ("% s", s); for (int j = 0; j <n; j ++) if (s [j] = '1' & j! = I-1) addedge (I, j + 1 + n, 1, bit [I] ^ bit [j + 1]);} for (int I = 1; I <= n; I ++) {addedge (ss, I,); addedge (I, tt,); addedge (I + n, tt );} printf ("% d \ n", Minflow (ss, tt);} return 0 ;}
Copyright statement: pursue your dreams and never give up! By-xdlove