Problem J. Mr.Panda and TubeMaster
Input file:
Output file:
Time limit:
Standard Input
Standard Ouptut
5 seconds
Mr.Panda loves playing games. Recently he falls in love with a game named TubeMaster.
In TubeMaster, players can build tubes in cells in a N × M table. Each cell can be either left empty or put in exact one of the following four kinds of tubes.
When two adjacent cells sharing a same edge are connected by tubes (e.g. the below picture), the player will gain some score (the score will be described in the Input section).
Some of the cells are considered as essential cells, which means each of such cells is required to contain a tube, otherwise the player loses the game.
Before the end of the game, the table needs to be a valid configuration, otherwise the player loses the game.
A valid configuration has to meet the following conditions:
• Each cell needs to contain either no tube or a tube which is in a tube cycle.
• Each of the essential cells needs to contain a tube.
Note that multiple tube cycles might appear in a valid configuration simultaneously and an empty
table can also be a valid configuration when there are no essential cells.
Page 15 of 21
The 2016 ACM-ICPC Asia China-Final Contest
In the tables above, the left one is a valid configuration while the middle one and the right one are not. (grey cells are essential cells)
Mr.Panda wants to win the game with a maximum accumulation of scores. Could you please help him calculate the value?
Input
The first line of the input gives the number of test cases, T.
T test cases follow. Each test case starts with a line containing two numbers separated by a space,
N and M indicating the number of rows and the number of columns of the table.
Then N lines follow, each line contains M − 1 numbers separated by spaces where ScoreCi,j (i.e. the jth number in the ith line) indicates the score of connecting the cell (i, j) and cell (i, j + 1) with tubes.
Then N − 1 lines follow, each line contains M numbers separated by spaces where ScoreRi,j (i.e. the jth number in the ith line) indicates the score of connecting the cell (i, j) and cell (i + 1, j) with tubes.
Then there will be a line containing a number, E the number of essential cells.
Finally E lines follow, each line contains two number separated by a space, Ri and Ci indicating
that the cell (Ri,Ci) is an essential cell. Output
For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the maximum accumulation of scores if Mr.Panda wins the game. If Mr.Panda cannot win the game, output “Impossible” for y.
Limits
• 1≤T ≤100.
• 1 ≤ N, M ≤ 30.
• −500 ≤ ScoreCi,j , ScoreRi,j ≤ 500.
• 0≤E≤100.
• 1≤Ri ≤N.
• 1≤Ci ≤M.
• For 40% of the test cases, N, M ≤ 10 holds.
Page 16 of 21
The 2016 ACM-ICPC Asia China-Final Contest
Sample input and output
Sample Input
Sample Output
2
44
0 0 -1 010
0 -1 -1 010 1010 -1 -1 0 0 1 1 -1 -1 1
33
23
00
00
000
2
11
23
题意:给出一个n*m的格子,有一些格子是关键格子,必须填入题目中给出的四种图案之一,非关键格子则可以不填,现在要求给出方案使得每个关键格子填入图案,并且使得每个图案形成闭合的一个或多个环,对于每个格子,若与上下左右连通则分别有对应获得的分数,求满足条件所能获得的最大分数,若不可能则输出impossible。
题解:quailty:黑白染色定向,把每个点拆成左右两部,相邻点按方向将出点的左部与入点的右部相连,连接非必要点的左右部表示该点可以不选,做最小费用最大流,不满流则impossible。
这一题也相当于找环,找环的套路做法就是对每个点找到后继,而这一题必须保证唯一后继,那么每个点拆点变成二分图,但是观察四种格子易知这一题与普通的环不一样,因为对于填入图案的每个格子必须在水平方向连接另一个格子,竖直方向连另一个格子,那么黑白染色,对于黑色格子连接上下(或左右)格子,白格连接左右(或上下)格子。
同时,有一些点不需要在环内,那么这种点多连一条自己出点到入点的容量为1费用为0的边就行了。