11 /* EDMONDS KARP para grafos dispersos... V ~<< E. */
13 int from
, to
, cap
, flow
;
18 // Siempre hay que inicializar esta variable:
20 void addEdge(int from
, int to
, int cap
, int flow
){
21 for(int i
= 0 ;i
< vec
[from
].size() ; i
++ )if( vec
[from
][i
] == to
){
22 aristas
[ind
[from
][i
]].cap
= cap
; return;
24 vec
[from
].push_back(to
), ind
[from
].push_back(indice
);
25 aristas
[indice
].from
= from
, aristas
[indice
].to
= to
;
26 aristas
[indice
].cap
= cap
, aristas
[indice
++].flow
= 0;
27 vec
[to
].push_back(from
), ind
[to
].push_back(indice
);
28 aristas
[indice
].from
= to
, aristas
[indice
].to
= from
;
29 aristas
[indice
].cap
= 0, aristas
[indice
++].flow
= 0;
33 bool isPath(int s
, int t
){
34 memset(prev
, -1, sizeof(prev
));
35 int qb
, qe
; qb
= qe
= 0;
36 prev
[q
[qe
++] = s
] = -2;
37 while( qb
< qe
&& prev
[t
] == -1 ){
38 for(int u
= q
[qb
++], i
=0, v
; i
< vec
[u
].size(); i
++){
39 if( prev
[v
= vec
[u
][i
]] != -1 ) continue;
40 if( aristas
[ind
[u
][i
]].flow
< aristas
[ind
[u
][i
]].cap
) prev
[q
[qe
++]=v
] = u
;
41 else if( aristas
[ind
[u
][i
]+(ind
[u
][i
]&1?-1:1)].flow
) prev
[q
[qe
++]=v
] = u
;
47 for(int i
=0 ;i
< vec
[v
].size() ; i
++ )if( vec
[v
][i
] == u
){
48 if( aristas
[ind
[v
][i
]].cap
> aristas
[ind
[v
][i
]].flow
){
49 return aristas
[ind
[v
][i
]].cap
-aristas
[ind
[v
][i
]].flow
;
51 return aristas
[ind
[v
][i
]+(ind
[v
][i
]&1?-1:1)].flow
;
54 void modify(int v
, int u
, int flow
){
55 for(int i
= 0; i
< vec
[v
].size() ; i
++) if( vec
[v
][i
] == u
){
56 if( aristas
[ind
[v
][i
]].cap
> aristas
[ind
[v
][i
]].flow
){
57 aristas
[ind
[v
][i
]].flow
+= flow
;
58 }else aristas
[ind
[v
][i
]+(ind
[v
][i
]&1?-1:1)].flow
-= flow
;
62 int augment(int s
,int t
){
65 for(int u
= t
, v
= prev
[u
]; v
!= u
; u
= v
, v
= prev
[u
]) flow
= min( flow
, r(v
,u
));
66 for(int u
= t
, v
= prev
[u
]; v
!= u
; u
= v
, v
= prev
[u
]){
71 int edmondsKarp2(int s
, int t
){
73 while( isPath(s
,t
) )flow
+= augment(s
,t
);
80 while(scanf("%i", &N
)!=EOF
){
81 for(i
=0;i
<220;i
++) vec
[i
].clear(), ind
[i
].clear();
85 addEdge(i
+1, i
+1+N
,k
,0);
90 scanf("%i %i %i", &a
, &b
, &c
);
94 scanf("%i %i", &B
, &D
);
101 addEdge(N
+c
,2*N
+1,INF
,0);
103 // printf("%i\n", indice);
104 int flow
= edmondsKarp2(0,2*N
+1);
105 printf("%i\n", flow
);