zoukankan      html  css  js  c++  java
  • hdu 3657 Game 最小割

    首先,这种相邻格子的问题都会联系到二分图:横纵坐标和为偶数的在左边,横纵坐标和为奇数的在右边。
    构图如下:

    原点和左边的点相连接,容量是其权值。右边的点和汇点连接,容量是其权值(如果某点为必选的,则权值为INF)。如果左边的点x和右边的点y相邻,连接x,y容量为2 * (x & y)。

    每个割都代表一种方案,最小化代价则会得到最大价值。

    #include <cstdio>
    #include <cstring>
    #include <vector>
    #include <queue>
    using namespace std;
    #define maxn 3000
    #define INF 100000
    struct Edge
    {
    	int from, to, cap, flow;
    };
    
    int n, m, s, t;
    int sum[55][55];
    int mark[55][55];
    int Count[55][55];
    vector<Edge> edges;    // 边数的两倍
    vector<int> G[maxn];   // 邻接表,G[i][j]表示结点i的第j条边在e数组中的序号
    bool vis[maxn];        // BFS使用
    int d[maxn];           // 从起点到i的距离
    int cur[maxn];         // 当前弧指针
    int min(int a,int b)
    {
    	if(a<b) return a;
    	else return b;
    }
    void AddEdge(int from, int to, int cap)
    {
    	int len;
    	Edge temp;
    	temp.from=from;temp.to=to;temp.cap=cap;temp.flow=0;
    	edges.push_back(temp);
    	temp.from=to;temp.to=from;temp.cap=0;temp.flow=0;
        edges.push_back(temp);
        len = edges.size();
        G[from].push_back(len-2);
        G[to].push_back(len-1);
    }
    bool BFS()
    {
    	memset(vis, 0, sizeof(vis));
        queue<int> Q;
        Q.push(s);
        vis[s] = 1;
        d[s] = 0;
        while(!Q.empty())
    	{
    		int x = Q.front(); Q.pop();
    		for(int i = 0; i < G[x].size(); i++)
    		{
    			Edge& e = edges[G[x][i]];
    			if(!vis[e.to] && e.cap > e.flow)
    			{
    				vis[e.to] = 1;
    				d[e.to] = d[x] + 1;
    				Q.push(e.to);
    			}
    		}
        }
        return vis[t];
    }
    int DFS(int x, int a)
    {
    	if(x == t || a == 0) return a;
        int flow = 0, f;
        for(int& i = cur[x]; i < G[x].size(); i++)
    	{
    		Edge& e = edges[G[x][i]];
    		if(d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap-e.flow))) > 0)
    		{
    			e.flow += f;
    			edges[G[x][i]^1].flow -= f;
    			flow += f;
    			a -= f;
    			if(a == 0) break;
    		}
        }
        if(flow==0) d[x]=-1;
        return flow;
    }
    int Maxflow()
    {
        int flow = 0;
        while(BFS())
    	{
    		memset(cur, 0, sizeof(cur));
    		flow += DFS(s, INF);
        }
        return flow;
    }
    int main()
    {
        int k;
        int i,j;
        int x,y;
        while(scanf("%d%d%d",&n,&m,&k)!=EOF)
        {
            int now=0;
            int ans=0;
            memset(mark,0,sizeof(mark));
            s=0;t=n*m+1;
            for(i=1;i<=n;i++)
                for(j=1;j<=m;j++)
                    Count[i][j]=++now;
            for(i=0;i<=t;i++) G[i].clear();
            for(i=1;i<=n;i++)
                for(j=1;j<=m;j++)
                {
                    scanf("%d",&sum[i][j]);
                    ans+=sum[i][j];
                }
            for(i=1;i<=k;i++)
            {
                scanf("%d%d",&x,&y);
                mark[x][y]=1;
            }
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=m;j++)
                {
                    if((i+j)%2==0)
                    {
                        if(mark[i][j]==0) AddEdge(s,Count[i][j],sum[i][j]);
                        if(mark[i][j]==1) AddEdge(s,Count[i][j],INF);
                        if(i-1>0) AddEdge(Count[i][j],Count[i-1][j],2*(sum[i][j]&sum[i-1][j]));
                        if(i+1<=n) AddEdge(Count[i][j],Count[i+1][j],2*(sum[i][j]&sum[i+1][j]));
                        if(j-1>0) AddEdge(Count[i][j],Count[i][j-1],2*(sum[i][j]&sum[i][j-1]));
                        if(j+1<=m) AddEdge(Count[i][j],Count[i][j+1],2*(sum[i][j]&sum[i][j+1]));
    
                    }
                    else
                    {
                        if(mark[i][j]==0) AddEdge(Count[i][j],t,sum[i][j]);
                        else AddEdge(Count[i][j],t,INF);
                    }
                }
            }
            ans=ans-Maxflow();
            printf("%d
    ",ans);
        }
        return 0;
    }
    
    


     

  • 相关阅读:
    iOS 关于NSNotificationCenter
    IOS UITableView reload 刷新某一个cell 或 section
    IOS AFNetWorking 设置超时时间
    IOS AFNetWorking
    IOS 十六进制字符串转换成UIColor
    IOS 长姿势---双击Home键
    IOS 关于 NSUserDefault
    方正璞华培训讲师
    localStorage使用总结
    Promise的简单用法
  • 原文地址:https://www.cnblogs.com/vermouth/p/3710142.html
Copyright © 2011-2022 走看看