lightoj 1171 - Knights in Chessboard (II) 【奇偶图 最小割】

来源:互联网 发布:网站搜索引擎优化案例 编辑:程序博客网 时间:2024/05/21 10:53
1171 - Knights in Chessboard (II)
PDF (English)StatisticsForum
Time Limit: 3 second(s)Memory Limit: 32 MB

Given an m x n chessboard where some of the cells are broken. Now you are about to place chess knights in the chessboard. You have to find the maximum number of knights that can be placed in the chessboard such that no two knights attack each other. You can't place knights in the broken cells.

Those who are not familiar with chess knights, note that a chess knight can attack eight positions in the board as shown in the picture below.

Input

Input starts with an integer T (≤ 125), denoting the number of test cases.

Each case starts with a blank line. The next line contains three integers m, n, K (1 ≤ m, n ≤ 200). Here m and n corresponds to the number of rows and the number of columns of the board respectively. Each of the next K lines will contain two integers x, y (1 ≤ x ≤ m, 1 ≤ y ≤ n) denoting that the cell(x, y)is broken already. No broken cell will be reported more than once.

Output

For each case of input, print the case number and the maximum number of knights that can be placed in the board considering the above restrictions.

Sample Input

Output for Sample Input

2

 

8 8 0

 

2 5 4

1 3

1 4

2 3

2 4

Case 1: 32

Case 2: 6

 


PROBLEM SETTER: JANE ALAM JAN


题意:给你一个n*m的棋盘和一个马的攻击范围,已知有k个位置不能放置任何东西。现在问你最多能放置多少个马。

不能用匹配,会TLE,不知道HK怎么样,没试。

思路:建立奇偶二分图,然后构建最小割模型,答案就是n*m - k - 最小割。

AC代码:

#include <cstdio>#include <cstring>#include <queue>#include <algorithm>#define INF 0x3f3f3f3f#define MAXN 40000+10#define MAXM 1000000+10using namespace std;struct Edge{    int from, to, cap, flow, next;};Edge edge[MAXM];int head[MAXN], edgenum;int dist[MAXN], cur[MAXN];bool vis[MAXN];void init(){    edgenum = 0;    memset(head, -1, sizeof(head));}void addEdge(int u, int v, int w){    Edge E1 = {u, v, w, 0, head[u]};    edge[edgenum] = E1;    head[u] = edgenum++;    Edge E2 = {v, u, 0, 0, head[v]};    edge[edgenum] = E2;    head[v] = edgenum++;}int S, T;int Map[201][201];int num[201][201];int n, m, k;bool judge(int x, int y){    return x >= 1 && x <= n && y >= 1 && y <= m;}void getMap(){    scanf("%d%d%d", &n, &m, &k);    memset(Map, 0, sizeof(Map));    for(int i = 0; i < k; i++)    {        int a, b;        scanf("%d%d", &a, &b);        Map[a][b] = 1;    }    init();    int Move[8][2] = {-2,-1, -2,1, -1,-2, 1,-2, -1,2, 1,2, 2,-1, 2,1};    int oddnum = 0, evennum = 0;    memset(num, 0, sizeof(num));    for(int i = 1; i <= n; i++)    {        for(int j = 1; j <= m; j++)        {            if(Map[i][j] == 0 && (i + j) & 1)                num[i][j] = ++oddnum;            else if(Map[i][j] == 0 && (i + j) % 2 == 0)                num[i][j] = ++evennum;        }    }    S = 0, T = oddnum + evennum + 1;    for(int i = 1; i <= n; i++)    {        for(int j = 1; j <= m; j++)        {            if((i + j) & 1 && Map[i][j] == 0)            {                addEdge(S, num[i][j], 1);                for(int p = 0; p < 8; p++)                {                    int x = i + Move[p][0];                    int y = j + Move[p][1];                    if(judge(x, y) && (x+y) % 2 == 0 && Map[x][y] == 0)                        addEdge(num[i][j], num[x][y] + oddnum, INF);                }            }            else if((i + j) % 2 == 0 && Map[i][j] == 0)                addEdge(num[i][j] + oddnum, T, 1);        }    }}bool BFS(int s, int t){    queue<int> Q;    memset(dist, -1, sizeof(dist));    memset(vis, false, sizeof(vis));    Q.push(s); dist[s] = 0; vis[s] = true;    while(!Q.empty())    {        int u = Q.front();        Q.pop();        for(int i = head[u]; i != -1; i = edge[i].next)        {            Edge E = edge[i];            if(!vis[E.to] && E.cap > E.flow)            {                dist[E.to] = dist[u] + 1;                if(E.to == t) return true;                vis[E.to] = true;                Q.push(E.to);            }        }    }    return false;}int DFS(int x, int a, int t){    if(x == t || a == 0) return a;    int flow = 0, f;    for(int &i = cur[x]; i != -1; i = edge[i].next)    {        Edge &E = edge[i];        if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(a, E.cap-E.flow), t)) > 0)        {            edge[i].flow += f;            edge[i^1].flow -= f;            flow += f;            a -= f;            if(a == 0) break;        }    }    return flow;}int Maxflow(int s, int t){    int flow = 0;    while(BFS(s, t))    {        memcpy(cur, head, sizeof(head));        flow += DFS(s, INF, t);    }    return flow;}int kcase = 1;void solve(){    getMap();    printf("Case %d: %d\n", kcase++, n*m - k - Maxflow(S, T));}int main(){    int t;    scanf("%d", &t);    while(t--){        solve();    }    return 0;}



0 0
原创粉丝点击