HDU 3395 Special Fish maximum "large" traffic charges

Source: Internet
Author: User

HDU 3395 Special Fish maximum "large" traffic charges

To calculate the maximum cost, you can convert the edge weight to the negative value to the minimum cost. However, this is still incorrect because the largest stream is preferred, but the answer does not necessarily appear when the stream is full. Therefore, you need to add some edges (red edges in the middle) to make the answers full when they appear. Set the traffic of all edges to 1, as shown in. Obviously, the maximum cost is 1001, and 3 is returned if there is no red edge.


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PHByZSBjbGFzcz0 = "brush: java;" >#include # include # Include # Include # Include # Include # Include # Include # Include # Pragma comment (linker, "/STACK: 1024000000"); # define EPS (1e-8) # define LL long # define ULL unsigned long # define _ LL _ int64 # define INF 0x3f3f3f3f # define Mod 6000007 using namespace std; struct E {int u, v, Max, cost, next;} edge [200100]; int head [310]; int cur [310]; int Top; void Link (int u, int v, int w, int cost) {edge [Top]. u = u; edge [Top]. v = v; edge [Top]. cost = cost; edge [Top]. max = w; edge [Top]. ne Xt = head [u]; head [u] = Top ++;} struct Q {int v; // bool operator <(const Q &) const {// return. cost <cost; // }}; void Updata (int site, int flow, int & cost) {for (int I = site; cur [I]! =-1; I = edge [cur [I] ^ 1]. v) {edge [cur [I]. max-= flow; edge [cur [I] ^ 1]. max + = flow; cost + = edge [cur [I]. cost * flow;} int dis [310]; int flow [310]; bool mark [310]; queueQ; int Spfa (int S, int T, int n, int & cost) {while (q. empty () = false) q. pop (); memset (mark, false, sizeof (bool) * (n + 2); memset (dis, INF, sizeof (int) * (n + 2 )); memset (flow, INF, sizeof (int) * (n + 2); dis [S] = 0; Q f, t; f. v = S; dis [S] = 0; cur [S] =-1; mark [S] = true; q. push (f); while (q. empty () = false) {f = q. front (); q. pop (); mark [f. v] = false; for (int p = head [f. v]; p! =-1; p = edge [p]. next) {t. v = edge [p]. v; if (edge [p]. max & dis [t. v]> dis [f. v] + edge [p]. cost) {cur [t. v] = p; dis [t. v] = dis [f. v] + edge [p]. cost; flow [t. v] = min (flow [f. v], edge [p]. max); if (mark [t. v] = false) {mark [t. v] = true; q. push (t) ;}}} if (dis [T]! = INF) {Updata (T, flow [T], cost); return flow [T];} return 0;} int Cal_MaxFlow_MinCost (int S, int T, int n) {int f = 0, cost = 0, temp; do {temp = Spfa (S, T, n, cost); f ++ = temp;} while (temp ); printf ("% d \ n",-cost); return cost;} int val [110]; int main () {int n; int u, v, w, I, j, x; while (scanf ("% d", & n) {memset (head,-1, sizeof (int) * (2 * n + 3); Top = 0; for (I = 1; I <= n; ++ I) {scanf ("% d ", & val [I]);} int S = 1, T = 2 * n + 2; for (I = 1; I <= n; ++ I) {Link (S, I +, 0); Link (I + 1, S,); Link (n + I + 1, T,); Link (T, n + I +, 0); Link (I + 1, T,); Link (T, I +, 0);} for (I = 1; I <= n; ++ I) {for (j = 1; j <= n; ++ j) {scanf ("% 1d", & x ); if (x) {Link (I + 1, j + n + 1, 1,-(val [I] ^ val [j]); Link (j + n + 1, I +, (val [I] ^ val [j]) ;}} Cal_MaxFlow_MinCost (S, T, T) ;}return 0 ;}

Related Article

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.