hdu3657 Game(最大点权独立集||最小割)

来源:互联网 发布:会计软件的功能 编辑:程序博客网 时间:2024/05/20 11:37


http://acm.hdu.edu.cn/showproblem.php?pid=3657

题意:方格取数,求取出的数和最大。取相邻的数要付出权值相与的代价,同时还要指定一些格子必须要取到。


思路:整体思路和hdu1565一样,看我的理解。保证某个点必须取那就连一条割不动的边,我们知道割不动的不会被算在最小花费,也就被算在最后的最大点权独立集。至于中间相邻格子的代价,把原先的割不动改为相应权值即可。注意这些权值要在图的信息全部输完后再拉边,还WA了几次。


#include <stdio.h>#include <algorithm>#include <stdlib.h>#include <string.h>#include <iostream>#include <queue>using namespace std;typedef long long LL;const int N = 2515;const int INF = 0x3f3f3f3f;int head[N], dis[N], cur[N], G[55][55];bool vis[N];int n, m, k, cnt, sum;struct Edge{    int to, cap, flow, next;}edge[N*N];void add(int u, int v, int w){    edge[cnt] = (struct Edge){v, w, 0, head[u]};    head[u] = cnt++;    edge[cnt] = (struct Edge){u, 0, 0, head[v]};    head[v] = cnt++;}bool bfs(int start, int endd){    memset(dis, -1, sizeof(dis));    memset(vis, false, sizeof(vis));    queue<int>que;    dis[start] = 0;    vis[start] = true;    que.push(start);    while(!que.empty())    {        int u = que.front();        que.pop();        for(int i = head[u]; i != -1; i = edge[i].next)        {            Edge E = edge[i];            if(!vis[E.to] && E.flow<E.cap)            {                dis[E.to] = dis[u]+1;                vis[E.to] = true;                if(E.to == endd) return true;                que.push(E.to);            }        }    }    return false;}int dfs(int x, int res, int endd){if(x == endd || res == 0) return res;int flow = 0, f;for(int& i = cur[x]; i != -1; i = edge[i].next){Edge E = edge[i];if(dis[E.to] == dis[x]+1){    f = dfs(E.to, min(res, E.cap-E.flow), endd);            if(f>0)            {                edge[i].flow += f;                edge[i^1].flow -= f;                flow += f;                res -= f;                if(res == 0) break;            }}}return flow;}int max_flow(int start, int endd){    int flow = 0;    while(bfs(start, endd))    {        memcpy(cur, head, sizeof(head));        flow += dfs(start, INF, endd);    }    return flow;}void init(){    cnt = 0;    memset(head, -1, sizeof(head));}void getmap(){    int s = 0, t = n*m+1;    sum = 0;    for(int i = 1; i <= n; i++)        for(int j = 1; j <= m; j++)        {            scanf("%d", &G[i][j]);            sum+=G[i][j];            if((i+j)%2 == 1) add(s, (i-1)*m+j, G[i][j]);            else add((i-1)*m+j, t, G[i][j]);        }    for(int i = 1; i <= n; i++)        for(int j = 1; j <= m; j++)        {            if((i+j)%2 == 1)            {                if(j>1) add((i-1)*m+j, (i-1)*m+j-1, 2*(G[i][j]&G[i][j-1]));//左边                if(j<m) add((i-1)*m+j, (i-1)*m+j+1, 2*(G[i][j]&G[i][j+1]));//右边                if(i>1) add((i-1)*m+j, (i-1-1)*m+j, 2*(G[i][j]&G[i-1][j]));//上边                if(i<n) add((i-1)*m+j, (i-1+1)*m+j, 2*(G[i][j]&G[i+1][j]));//下边            }        }    int u, v;    for(int i = 1; i <= k; i++)    {        scanf("%d%d", &u, &v);        if((u+v)%2 == 1) add(s, (u-1)*m+v, INF);        else add((u-1)*m+v, t, INF);    }}int main(){  //  freopen("in.txt", "r", stdin);    while(~scanf("%d%d%d", &n, &m, &k))    {        init();        getmap();        int ans = max_flow(0, n*m+1);        printf("%d\n", sum-ans);    }    return 0;}


0 0