inputs: Solve in Python:
Chef just come up with a very good idea for his business. He needs to hire two group of software engineers. Each group of engineers will work on completely different things and people from different groups don't want to disturb (and even hear) each other. Chef has just rented a whole floor for his purposes in business center "Cooking Plaza". The floor is a rectangle with dimensions N over M meters. For simplicity of description the floor's structure, let's imagine that it is split into imaginary squares of size 1x1 called "cells".
The whole floor is split into rooms (not necessarily rectangular). There are some not noise-resistant walls between some of the cells. Two adjacent cells belong to the same room if they don't have the wall between them. Cells are considered adjacent if and only if they share an edge. Also, we say that relation "belong to the same room" is transitive. In other words we say that if cells A and B belong to the same room and B and C belong to the same room then A and C belong to the same room.
So we end up having a partition of the floor into rooms. It also means, that each point on the floor belongs to some room.
Chef have to distribute the rooms between engineers of two groups. Engineers from the different groups cannot seat in the same room. If engineers from a different groups seat in adjacent rooms, the walls these rooms share have to be noise-resistant. The cost of having one meter of wall isolated is K per month. Due to various reasons Chef has to pay an additional cost for support of each of the room (e.g. cleaning costs money as well). Interesting to know that support cost for a particular room may differ depending on engineers of which group seat in this room.
Chef doesn't know the number of people he needs in each group of engineers so he wants to minimize the money he needs to pay for all the floor rent and support. He will see how it goes and then redistribute the floor or find another floor to rent or whatever. Either way, you don't need to care about this.
Please pay...
targets: import sys
 
def findRoom(x,y,i):
 R = [(x,y)]
 GRID[x][y] = i
 for n in R:
  GRID[n[0]][n[1]] = i
  if n[0]>0 and GRID[n[0]-1][n[1]]==0 and H[n[0]-1][n[1]]:
   GRID[n[0]-1][n[1]] = i
   R.append((n[0]-1,n[1]))
  if n[0]<N-1 and GRID[n[0]+1][n[1]]==0 and H[n[0]][n[1]]:
   GRID[n[0]+1][n[1]] = i
   R.append((n[0]+1,n[1]))
  if n[1]>0 and GRID[n[0]][n[1]-1]==0 and V[n[0]][n[1]-1]:
   GRID[n[0]][n[1]-1] = i
   R.append((n[0],n[1]-1))
  if n[1]<M-1 and GRID[n[0]][n[1]+1]==0 and V[n[0]][n[1]]:
   GRID[n[0]][n[1]+1] = i
   R.append((n[0],n[1]+1))

def roomPrice(r):
 wall_price_0 = wall_price_1 = 0
 for i in range(R):
  if C[i][r] and T[i] != 1:
   wall_price_0 += C[i][r]*K
  else:
   wall_price_1 += C[i][r]*K
 return [wall_price_0 + Rooms[r][0], wall_price_1 + Rooms[r][1]]

def total_price():
 price = 0
 for r in range(R):
  for i in range(r):
   if C[i][r] and T[i] != T[r]:
    price += C[i][r]*K
    price += Rooms[r][T[r]-1]
 return price

def solve(r):
 if r==R:
  return 0
 wall_price_0 = 0
 wall_price_1 = 0
 for i in range(r):
  if C[i][r] and T[i] != 1:
   wall_price_0 += C[i][r]*K
  else:
   wall_price_1 += C[i][r]*K
 if T[r]!=0:
  return [wall_price_0,wall_price_1][T[r]-1]+Rooms[r][T[r]-1]+solve(r+1) 
 T[r] = 1
 result = solve(r+1)+wall_price_0+Rooms[r][0]
 T[r] = 2
 result = min(solve(r+1)+wall_price_1+Rooms[r][1], result)
 T[r] = 0
 return result
 
f = sys.stdin
N,M,W,K,R = list(map(int, f.readline().split(' ')))
T = [0] * R
GRID = list(map(list,[[0]*M]*N))
H = list(map(list,[[1]*M]*N))
V = list(map(list,[[1]*M]*N))
Walls = []
for _ in range(W):
 x0,y0,x1,y1 = list(map(int, f.readline().split(' ')))
 x0 -= 1
 x1 -= 1
 y0 -= 1
 y1 -= 1
 if x0==x1:
  V[x0][y0] = 0
 else:
  H[x0][y0] = 0
 Walls.append([x0,y0,x1,y1]) 
Rooms = []
for i in range(R):
 x,y,t1,t2 = list(map(int, f.readline().split(' ')))
 findRoom(x-1,y-1,i+1)
 Rooms.append([t1,t2])
C = list(map(list,[[0]*R]*R))
for w in Walls:
 r1 = GRID[w[0]][w[1]]-1 
 r2 = GRID[w[2]][w[3]]-1
 C[r1][r2] += 1
 C[r2][r1] += 1
Stable = [False]*R
for r in range(R):
...