fzu2143 Board Game

 Problem 2143 Board Game

Accept: 54    Submit: 151
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem Description

Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of the board which is own by Fat brother is consisting of an integer 0. At each turn, he can choose two adjacent grids and add both the integer inside them by 1. But due to some unknown reason, the number of each grid can not be large than a given integer K. Also, Maze has already drown an N*M board with N*M integers inside each grid. What Fat brother would like to do is adding his board to be as same as Maze’s. Now we define the different value of two boards A and B as:

Now your task is to help Fat brother the minimal value of S he can get.

 Input

The first line of the date is an integer T, which is the number of the text cases.

Then T cases follow, each case contains three integers N, M and K which are mention above. Then N lines with M integers describe the board.

1 <= T <= 100, 1 <= N, M, K <= 9

0 <= the integers in the given board <= 9

 Output

For each case, output the case number first, then output the minimal value of S Fat brother can get.

 Sample Input

5
2 2 9
3 4
2 3
1 3 9
4 6 4
1 1 9
9
3 3 5
1 2 3
4 5 6
7 8 9
3 3 9
1 2 3
4 5 6
7 8 9

 Sample Output

Case 1: 0
Case 2: 2
Case 3: 81
Case 4: 33
Case 5: 5


最小费用流
每次操作只更改相邻的两个数,可看作棋盘模型,黑白两色
对于a[i,j]  ,  对答案的贡献为
a[i,j]^2 - 2*a[i,j]*b[i,j] + b[i,j]^2   
b[i,j]^2 为常项   ,  对于a[i,j] ,由选p-1到p时,  答案增加了 2p - 1 -  2*b
对于所有白色点 ,添加源点至白点的K条边,花费为 2p-1-2*b, 流量为1,
对于所有黑色点 ,添加黑点至汇点的K条边,花费为 2p-1-2*b, 流量为1,
任意相邻的黑白点之间添加花费为0,流量无穷的一条边,
在图上跑最小费用流,当d[T]>=0时,对答案已无影响,即可退出

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>

using namespace std;

struct Edge
{
    int from, to , cap, flow, cost , next;
};

const int inf = 0x3f3f3f3f;

int n,m,K;
Edge edge[3010];
int head[110];
int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};
int map[11][11];
int d[110],a[110],p[110];
bool vis[110];
int ans,cnt;

void add(int from, int to , int cap, int cost)
{
    edge[cnt].from = from;
    edge[cnt].to = to;
    edge[cnt].cap =cap;
    edge[cnt].flow = 0;
    edge[cnt].cost = cost;
    edge[cnt].next = head[from];
    head[from] = cnt ++;

    edge[cnt].from = to;
    edge[cnt].to = from;
    edge[cnt].cap = 0;
    edge[cnt].flow = 0;
    edge[cnt].cost = -cost;
    edge[cnt].next = head[to];
    head[to] = cnt ++;
}

bool bound(int x,int y)
{
    return ( x>=1 && x<=n && y>=1 && y<=m );
}

bool spfa(int S, int T, int &flow, int &cost)
{
    memset(d,63,sizeof(d));
    memset(vis, 0, sizeof(vis));
    queue <int> q;
    d[S] = 0;  a[S] = inf;
    vis[S] = 1;  p[S] = 0;
    q.push(S);
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        int i=head[u];
        while (i!=-1)
        {
            int v= edge[i].to;
            if (edge[i].cap>edge[i].flow && d[v]>d[u]+edge[i].cost)
            {
                d[v] = d[u] +edge[i].cost;
                p[v] = i;
                a[v] = min( a[u], edge[i].cap - edge[i].flow);
                if (!vis[v])
                {
                    q.push(v);
                    vis[v] =1;
                }
            }
            i = edge[i].next;
        }
        vis[u]=0;
    }
    if (d[T]>=0)  return false;
    flow += a[T];
    cost += d[T] * a[T];
    int u= T;
    while ( u!=S )
    {
        edge[ p[u] ] .flow +=a[T];
        edge[ p[u]^1 ].flow -=a[T];
        u=edge[ p[u] ].from;
    }
    return true;
}

void Mincost (int S, int T)
{
    int flow = 0 , cost = 0;
    while ( spfa(S, T, flow, cost) );
    ans += cost;
}

int main()
{
    int T,cas=0;
    scanf("%d",&T);
    while (T--)
    {
        memset(head,-1,sizeof(head));
        cnt =ans = 0;
        scanf("%d%d%d",&n,&m,&K);
        int idx=0, x;
        int st= 0,  en = n*m+1;
        for (int i=1; i<=n; i++)
            for (int j=1; j<=m; j++)
            {
                scanf("%d",&x);
                ans += x * x;
                map[i][j]= ++idx;
                for (int k=1; k<=K; k++)
                    if ( i % 2==j % 2)
                        add( st, map[i][j], 1 , 2*k -1 - 2*x );
                    else
                        add( map[i][j],en , 1 , 2*k -1 - 2*x );
            }
        for (int i=1; i<=n; i++)
            for (int j=1; j<=m; j++)
            {
                if ( i % 2 == j % 2 )
                    for (int k=0; k<4; k++)
                    {
                        int tx=  i + dx[k];
                        int ty=  j + dy[k];
                        if ( !bound( tx,ty) )  continue;
                        add( map[i][j], map[tx][ty], inf, 0);
                    }
            }
        Mincost(st, en);
        printf("Case %d: %d\n",++cas,ans);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值