hdu 3820 Golden Eggs(最小割,最大点权独立集+拆点)

来源:互联网 发布:知乎 金庸 黄蓉 编辑:程序博客网 时间:2024/05/17 18:15

Golden Eggs

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 337    Accepted Submission(s): 192


Problem Description
There is a grid with N rows and M columns. In each cell you can choose to put a golden or silver egg in it, or just leave it empty. If you put an egg in the cell, you will get some points which depends on the color of the egg. But for every pair of adjacent eggs with the same color, you lose G points if there are golden and lose S points otherwise. Two eggs are adjacent if and only if there are in the two cells which share an edge. Try to make your points as high as possible.
 

Input
The first line contains an integer T indicating the number of test cases.
There are four integers N, M, G and S in the first line of each test case. Then 2*N lines follows, each line contains M integers. The j-th integer of the i-th line Aij indicates the points you will get if there is a golden egg in the cell(i,j). The j-th integer of the (i+N)-th line Bij indicates the points you will get if there is a silver egg in the cell(i,j).

Technical Specification
1. 1 <= T <= 20
2. 1 <= N,M <= 50
3. 1 <= G,S <= 10000
4. 1 <= Aij,Bij <= 10000
 

Output
For each test case, output the case number first and then output the highest points in a line.
 

Sample Input
22 2 100 1001 15 11 41 11 4 85 95100 100 10 1010 10 100 100
 

Sample Output
Case 1: 9Case 2: 225
 

题意:有n*m个格子,每个格子可以放一个金色的蛋或银色的蛋,若放金色,会得到相应的分数,若放银色,会得到一个不同的分数。规定若存在两个相邻格子的颜色一样,若都为金色,则要减去分数G,若都未银色,则要减去分数S。给出每个格子放金色蛋和银色蛋得到的相应分数,问最多能得到多少分。
思路:这题是方格取数的加强版,需要拆点。
将格子分为偶数点和奇数点,将每个点拆成k和k'
偶数点k表示该格子放金色,k'表示放银色。
奇数点k表示该格子放银色,k'表示放金色。
源点向每个k连一条边,容量为相应的点权,每个k'向汇点连一条边,容量也为相应的点权。每个k向k'连一条边,容量为INF,保证了不能同时放金色和银色。
然后偶数点的k连一条边到相邻格子的k',容量为G,表示若两个相邻格子的颜色都为金色,则它们都取的代价为G。
奇数点的k连一条边到相邻格子的k',容量为S,表示若两个相邻格子的颜色都为银色,则它们都取的代价为S。

最后答案为 总的点权 - 最小割


AC代码:
#include <iostream>#include <cmath>#include <cstdlib>#include <cstring>#include <cstdio>#include <queue>#include <ctime>#include <algorithm>#define ll __int64using namespace std;const int INF = 1000000000;const int maxn = 5005;int d[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};struct Edge{    int u, v, cap, flow, next;} et[maxn * maxn];int low[maxn], cnt[maxn], dis[maxn], cur[maxn], pre[maxn], eh[maxn];int s, t, n, m, G, S, num;int A[55][55], B[55][55];bool ok(int x, int y){    if(x >= 1 && x <= n && y >= 1 && y <= m) return true;    return false;}void init(){    memset(eh, -1, sizeof(eh));    num = 0;}void add(int u, int v, int cap, int flow){    Edge e = {u, v, cap, flow, eh[u]};    et[num] = e;    eh[u] = num++;}void addedge(int u, int v, int cap){    add(u, v, cap, 0);    add(v, u, 0, 0);}int isap(int s, int t, int nv){    int u, v, now, flow = 0;    memset(cnt, 0, sizeof(cnt));    memset(dis, 0, sizeof(dis));    memset(low, 0, sizeof(low));    for(u = 0; u <= nv; u++) cur[u] = eh[u];    low[s] = INF, cnt[0] = nv, u =s;    while(dis[s] < nv)    {        for(now = cur[u]; now != -1; now = et[now].next)            if(et[now].cap - et[now].flow && dis[u] == dis[v = et[now].v] + 1) break;        if(now != -1)        {            cur[u] = pre[v] = now;            low[v] = min(et[now].cap - et[now].flow, low[u]);            u = v;            if(u == t)            {                for(; u != s; u = et[pre[u]].u)                {                    et[pre[u]].flow += low[t];                    et[pre[u]^1].flow -= low[t];                }                flow += low[t];                low[s] = INF;            }        }        else        {            if(--cnt[dis[u]] == 0) break;            dis[u] = nv, cur[u] = eh[u];            for(now = eh[u]; now != -1; now = et[now].next)                if(et[now].cap - et[now].flow && dis[u] > dis[et[now].v] + 1)                    dis[u] = dis[et[now].v] + 1;            cnt[dis[u]]++;            if(u != s) u = et[pre[u]].u;        }    }    return flow;}int main(){    int tt, ca = 0;    scanf("%d", &tt);    while(tt--)    {        scanf("%d%d%d%d", &n, &m, &G, &S);        init();        s = 0;        t = 2 * n * m + 1;        int sum = 0;        for(int i = 1; i <= n; i++)        for(int j = 1; j <= m; j++)        {            scanf("%d", &A[i][j]);            sum += A[i][j];        }        for(int i = 1; i <= n; i++)        for(int j = 1; j <= m; j++)        {            scanf("%d", &B[i][j]);            sum += B[i][j];        }        for(int i = 1; i <= n; i++)        for(int j = 1; j <= m; j++)        {            int u = (i - 1) * m + j, v = u + n * m;            addedge(u, v, INF);            if((i + j) & 1)            {                addedge(s, u, B[i][j]);                addedge(v, t, A[i][j]);                for(int k = 0; k < 4; k++)                {                    int nx = i + d[k][0], ny = j + d[k][1];                    if(ok(nx, ny)) addedge(u, (nx - 1) * m + ny + n * m, S);                }            }            else            {                addedge(s, u, A[i][j]);                addedge(v, t, B[i][j]);                for(int k = 0; k < 4; k++)                {                    int nx = i + d[k][0], ny = j + d[k][1];                    if(ok(nx, ny)) addedge(u, (nx - 1) * m + ny + n * m, G);                }            }        }        printf("Case %d: %d\n", ++ca, sum - isap(s, t, t + 1));    }    return 0;}



0 0
原创粉丝点击