HDU 4292:food (maximum flow)

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=4292

Test instructions: It's almost like a cow, but there are a variety of foods.

Idea: Because of the variety of food, so the source and meeting point capacity to be changed. and Dinic and Tle, with ISAP.

#include <cstdio>#include<algorithm>#include<iostream>#include<cstring>#include<string>#include<cmath>#include<queue>#include<vector>#include<map>#include<Set>using namespacestd;#defineINF 0x3f3f3f3f#defineN 910typedefLong LongLl;typedefLong LongLL;structEdge {intV, Cap, NXT; Edge () {} Edge (intVintCapintNXT): V (v), cap (CAP), NXT (NXT) {}}edge[n*N];inttot, Cur[n], dis[n], pre[n], head[n], gap[n], S, T;voidADD (intUintVintcap) {Edge[tot]= Edge (V, Cap, Head[u]); Head[u] = tot++; Edge[tot]= Edge (U,0, Head[v]); HEAD[V] = tot++;}voidBFS () {Queue<int>que;  while(!que.empty ()) Que.pop (); memset (DIS,-1,sizeof(DIS)); Memset (Gap,0,sizeof(GAP)); Dis[t]=0; gap[0]++; Que.push (T);  while(!Que.empty ()) {        intU =Que.front (); Que.pop ();  for(inti = Head[u]; ~i; i =edge[i].nxt) {            intv =edge[i].v; if(Dis[v] = =-1)Continue; DIS[V]= Dis[u] +1; GAP[DIS[V]]++;        Que.push (v); }    }}intISAP (intN) {memcpy (cur, head,sizeof(cur));    BFS (); intU = pre[s] = S, ans =0, I;  while(Dis[s] <N) {if(U = =T) {intFlow =INF, index;  for(inti = S; I! = T; i =edge[cur[i]].v) {Edge& e =Edge[cur[i]]; if(E.cap <flow) {Flow= E.cap; index =i; }            }             for(inti = S; I! = T; i =edge[cur[i]].v) {Edge[cur[i]].cap-=flow; Edge[cur[i]^1].cap + =flow; } ans+ = flow; U =index; }         for(i = cur[u]; ~i; i =edge[i].nxt)if(DIS[EDGE[I].V] = = Dis[u]-1&& Edge[i].cap >0)                 Break; if(~i) {Cur[u]=i; PRE[EDGE[I].V]=u; U=edge[i].v; } Else {            intMD =N; if(--gap[dis[u]] = =0) Break;  for(inti = Head[u]; ~i; i =edge[i].nxt) {                if(MD > DIS[EDGE[I].V] && edge[i].cap >0) {MD= DIS[EDGE[I].V]; Cur[u] =i; }            }            ++GAP[DIS[U] = MD +1]; U=Pre[u]; }    }    returnans;}intMain () {intN, F, D;  while(~SCANF ("%d%d%d", &n, &f, &d)) {S=0, T =2* n + F + D +1; memset (Head,-1,sizeof(head)); Tot=0;intAChars[ -];  for(inti =1; I <= F; i++) {scanf ("%d", &a); ADD (S, I+2*N, a); }         for(inti =1; I <= D; i++) {scanf ("%d", &a); ADD (i+2* n +F, T, a); }         for(inti =1; I <= N; i++) {Add (I, I+ N,1); scanf ("%s", S +1);  for(intj =1; J <= F; J + +) {                if(S[j] = ='Y') {ADD (2* n + j, I,1); }            }        }         for(inti =1; I <= N; i++) {scanf ("%s", S +1);  for(intj =1; J <= D; J + +) {                if(S[j] = ='Y') {ADD (i+ N,2* n + j + F,1); }}} printf ("%d\n", ISAP (T +1)); }    return 0;}

HDU 4292:food (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.