hdu1565 方格取数(1)&&hdu1569 方格取数(2)(最小割)

来源:互联网 发布:蚁群算法基本思想 编辑:程序博客网 时间:2024/06/06 00:51


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

题意:中文题不解释。


思路:最大点权独立集。注意二分匹配能处理的是最大点独立集,带权的就只能用最大流了。刚开始看是求取出的数和最大,那直接用最大流呗,不知道怎么建边。看别人的才知道这些带权的都是独立点,我们是无法对独立点求最大流的。最大点权独立集=点权总和-最小点权覆盖集。二分匹配中最小顶点覆盖集是最大匹配数,这里又是什么鬼?其实是最小割值。


最小割的意义就是求取数方案的最小花费(前提是能取的都取,一个也不取不能算最小花费),同样满足相邻各自不能取(由于相邻各自不能取,只有两种取数方案)。这题由于上下左右相邻的数不能取,所以按照国际棋盘染色规律分为两个集合,那么这两个集合一个和源点s相连,一个和汇点t相连,边的权值就是数的值。一条路上两种元素满足互斥关系(有你没我),这就是判断是否是最小割问题的关键点!但是这个不同于上一个任务被执行,割一条边就行,这个是两个元素,虽然两个边同时不被割不可能,但是同时被割完全有可能啊!所以中间需要连一条无穷大的边,代表要想同时被割需要付出无穷大的代价。这是这道题比较特殊的一点。其他的思路就和上一题一样了。


有了思路知道怎么建图,这题就该被秒杀了。


#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 = 505;const int INF = 0x3f3f3f3f;int head[N], dis[N], cur[N], G[N][N];bool vis[N];int n, m, 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*n+1;    sum = 0;    for(int i = 1; i <= n; i++)        for(int j = 1; j <= n; j++)        {            scanf("%d", &G[i][j]);            sum+=G[i][j];            if((i+j)%2 == 1)            {                add(s, (i-1)*n+j, G[i][j]);                if(j>1) add((i-1)*n+j, (i-1)*n+j-1, INF);                if(j<n) add((i-1)*n+j, (i-1)*n+j+1, INF);                if(i>1) add((i-1)*n+j, (i-1-1)*n+j, INF);                if(i<n) add((i-1)*n+j, (i-1+1)*n+j, INF);            }            else            {                add((i-1)*n+j, t, G[i][j]);            }        }}int main(){  //  freopen("in.txt", "r", stdin);    while(~scanf("%d", &n))    {        init();        getmap();        int ans = max_flow(0, n*n+1);        printf("%d\n", sum-ans);    }    return 0;}




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

题意思路和上面一样,改个输入输出就行。



#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 = 2505;const int INF = 0x3f3f3f3f;int head[N], dis[N], cur[N], G[N][N];bool vis[N];int n, m, 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]);                if(j>1) add((i-1)*m+j, (i-1)*m+j-1, INF);                if(j<m) add((i-1)*m+j, (i-1)*m+j+1, INF);                if(i>1) add((i-1)*m+j, (i-1-1)*m+j, INF);                if(i<n) add((i-1)*m+j, (i-1+1)*m+j, INF);            }            else            {                add((i-1)*m+j, t, G[i][j]);            }        }}int main(){  //  freopen("in.txt", "r", stdin);    while(~scanf("%d%d", &n, &m))    {        init();        getmap();        int ans = max_flow(0, n*m+1);        printf("%d\n", sum-ans);    }    return 0;}



0 0