18 #define forn(i, n) for(int i=0;i<int(n);i++)
19 #define FOR(i, a, b) for(int i=(a);i<int(b);i++)
20 #define RFOR(i, b, a) for(int i=(b);i>int(a);i--)
21 #define foreach(it, c) for(__typeof((c).begin()) it = (c).begin();it!=(c).end();++it)
22 #define ALL(x) (x).begin(),(x).end()
23 #define SIZE(x) (int)(x).size()
24 #define SORT(x) sort(ALL(x))
26 #define VI vector<int>
27 #define VS vector<string>
29 #define ISS istringstream
30 #define OSS ostringstream
32 // NUNCA DEFINIR int max....
37 # define MAXVERTICES 1802
38 # define Q MAXVERTICES+1
40 # define min(a,b) (((a)<(b))? (a):(b))
43 int f
[MAXVERTICES
][MAXVERTICES
];
44 int cf
[MAXVERTICES
][MAXVERTICES
];
45 int valor(int r
, int c
, int i
){
46 return (r
*cols
+c
)*2+1+i
;
49 int dx
[5] = {-1, 0, 1, 0, 0};
50 int dy
[5] = {0, 1, 0, -1, 0};
51 int adentro(int r
, int c
){ if( r
>= 0 && c
>= 0 && r
< rows
&& c
< cols
) return 1; return 0;}
52 void setCapacity(void)
54 memset(cf
, 0, sizeof(cf
));
67 if( vec
[i
][j
] == '~' ) continue;
68 if( vec
[i
][j
] == '*' ){
69 V
.push_back(valor(i
,j
,0));
70 cf
[0][valor(i
,j
,0)] = 1;
71 cf
[valor(i
,j
,0)][valor(i
,j
,1)] = 1;
76 if(!adentro(r
,c
))continue;
77 if( vec
[r
][c
] != '~' && vec
[r
][c
] != '*'){
78 cf
[valor(i
,j
,1)][valor(r
,c
,0)] = 1;
82 if( vec
[i
][j
] == '@' ){
83 cf
[valor(i
,j
,0)][valor(i
,j
,1)] = 2000;
88 if(!adentro(r
,c
))continue;
89 if( vec
[r
][c
] != '~' && vec
[r
][c
] != '*'){
90 cf
[valor(i
,j
,1)][valor(r
,c
,0)] = 2000;
94 if( vec
[i
][j
] == '.' ){
95 cf
[valor(i
,j
,0)][valor(i
,j
,1)] = 1;
100 if(!adentro(r
,c
))continue;
101 if( vec
[r
][c
] != '~' && vec
[r
][c
] != '*'){
102 cf
[valor(i
,j
,1)][valor(r
,c
,0)] = 1;
106 if( vec
[i
][j
] == '#' ){
107 cf
[valor(i
,j
,0)][valor(i
,j
,1)] = 2000;
108 cf
[valor(i
,j
,1)][n
] = p
;
113 if(!adentro(r
,c
))continue;
114 if( vec
[r
][c
] != '~' && vec
[r
][c
] != '*'){
115 cf
[valor(i
,j
,1)][valor(r
,c
,0)] = 2000;
121 /* get input and make the graph */
122 /* 0 : source , n : sink */
124 void initialize(void)
135 int cost
[MAXVERTICES
],p
[MAXVERTICES
];
144 if(front
==Q
) front
=0;
150 for(int k
= 0 ; k
< 5; k
++ ){
153 for(int h
=0;h
<2;h
++){
154 int j
= valor(rr
,cc
, h
);
155 if(cf
[i
][j
]>0 && cost
[j
]==-1){
164 if(cf
[i
][j
]>0 && cost
[j
]==-1){
171 if(cf
[i
][j
]>0 && cost
[j
]==-1){
178 for(int h
=0;h
<V
.size();h
++){
180 if(cf
[i
][j
]>0 && cost
[j
]==-1){
189 if(cost
[n
]==-1) return 0;
193 cfp
=min(cfp
,cf
[p
[i
]][i
]);
199 f
[i
][p
[i
]]=-f
[p
[i
]][i
];
212 for(totflow
=0,i
=0;i
<=n
;i
++)
218 while(scanf("%i %i %i", &rows
, &cols
, &p
)!= EOF
){
219 printf("%i\n", maxFlow());