zoukankan      html  css  js  c++  java
  • 【GMOJ4020】Revolution

    题目

    题目链接:https://gmoj.net/senior/#main/show/4020
    地图是个矩形的网格。
    可以花费一定金钱在一些格子投资。
    被投资的格子或者四连通的格子都被投资的话,我就可以获得该格子的收益。
    利益最大化是作为商人的基本准则,但这是计算机的任务,拜托您了。

    思路

    这种网格图并且相邻格子有限制的很容易想到最小割模型。
    先不管四联通格子投资的情况,先设\(ans=\sum^{n}_{i=1}\sum^{m}_{j=1}val[i][j]\),那么一个点要么割掉它的代价(买这个格子),要么割掉它的价值(不买这个格子)。
    那么考虑四联通的情况,一个点如果我们不割掉它的代价依然希望获得它的价值,那么必须将四联通的格子的代价全部割掉。
    那么我们将这个图黑白染色,每个点拆成两个点\(a,b\),从\(a\to b\)连一条流量为\(val\)的边,对于白点,从\(S\to a\)连一条流量为\(cost\)的边,黑点从\(b\to T\)连一条流量为\(cost\)的边。
    然后每一个白点的\(a,b\)分别向四周的黑点的\(a,b\)连一条流量为\(+\infty\)的边。这样如果一个点的四联通的格子的\(cost\)全部被割掉了,那么这个点自然也没有流量了。

    代码

    #include <queue>
    #include <cstdio>
    #include <cctype>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    const int P=30,N=1500,M=800010,Inf=1e9;
    const int dx[]={0,0,0,-1,1},dy[]={0,-1,1,0,0};
    int n,m,S,T,tot=1,sum,maxflow,c[P][P],v[P][P],dep[N],head[N],cur[N];
    bool vis[N];
    char ch;
    
    struct edge
    {
    	int next,to,flow;
    }e[M*2];
    
    int ID(int x,int y)
    {
    	return (x-1)*m+y;
    }
    
    void add(int from,int to,int flow)
    {
    	e[++tot].to=to;
    	e[tot].flow=flow;
    	e[tot].next=head[from];
    	head[from]=tot;
    	swap(from,to);
    	e[++tot].to=to;
    	e[tot].flow=0;
    	e[tot].next=head[from];
    	head[from]=tot;
    }
    
    bool read(int &x)
    {
    	if (isdigit(ch)) x=ch-'0';
    	else if (ch>='a' && ch<='z') x=ch-'a'+10;
    	else if (ch>='A' && ch<='Z') x=ch-'A'+36;
    	else return 0;
    	return 1;
    }
    
    bool bfs()
    {
    	memset(dep,0x3f3f3f3f,sizeof(dep));
    	memcpy(cur,head,sizeof(cur));
    	queue<int> q;
    	q.push(S);
    	dep[S]=1;
    	while (q.size())
    	{
    		int u=q.front();
    		q.pop();
    		for (int i=head[u];~i;i=e[i].next)
    		{
    			int v=e[i].to;
    			if (dep[v]>dep[u]+1 && e[i].flow)
    			{
    				dep[v]=dep[u]+1;
    				q.push(v);
    			}
    		}
    	}
    	return dep[T]<Inf;
    }
    
    int dfs(int x,int flow)
    {
    	int low=0,used=0;
    	if (x==T)
    	{
    		maxflow+=flow;
    		return flow;
    	}
    	for (int i=cur[x];~i;i=e[i].next)
    	{
    		int y=e[i].to;
    		cur[x]=i;
    		if (dep[y]==dep[x]+1 && e[i].flow)
    		{
    			low=dfs(y,min(e[i].flow,flow-used));
    			if (low)
    			{
    				e[i].flow-=low;
    				e[i^1].flow+=low;
    				used+=low;
    				if (used==flow) break;
    			}
    		}
    	}
    	return used;
    }
    
    void dinic()
    {
    	while (bfs()) dfs(S,Inf);
    }
    
    int main()
    {
    	memset(head,-1,sizeof(head));
    	scanf("%d%d",&n,&m);
    	for (int i=1;i<=n;i++)
    		for (int j=1;j<=m;j++)
    			while (ch=getchar())
    				if (read(c[i][j])) break;
    	for (int i=1;i<=n;i++)
    		for (int j=1;j<=m;j++)
    		{
    			while (ch=getchar())
    				if (read(v[i][j])) break;
    			sum+=v[i][j];
    		}
    	S=N-1; T=N-2;
    	for (int i=1;i<=n;i++)
    		for (int j=1;j<=m;j++)
    		{
    			add(ID(i,j),ID(i,j)+n*m,v[i][j]);
    			if ((i+j)&1)
    			{
    				add(S,ID(i,j),c[i][j]);
    				for (int k=1;k<=4;k++)
    				{
    					int x=i+dx[k],y=j+dy[k];
    					if (x<1 || x>n || y<1 || y>m) continue;
    					add(ID(i,j),ID(x,y),Inf);
    					add(ID(i,j)+n*m,ID(x,y)+n*m,Inf);
    				}
    			}
    			else add(ID(i,j)+n*m,T,c[i][j]);
    		}
    	dinic();
    	printf("%d\n",sum-maxflow);
    	return 0;
    }
    
  • 相关阅读:
    链接
    列表
    Android Studio AVD 虚拟机 联网 失败
    docker error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.29/containers/json: open //./pipe/docker_engine: The system cannot find the file specified. In the default daemon configuratio
    JSP Failed to load resource: net::ERR_INCOMPLETE_CHUNKED_ENCODING
    js jsp form
    intellij jsp 中文乱码
    [转载]在Intellij Idea中使用JSTL标签库
    windows pybloomfilter
    docker mysql
  • 原文地址:https://www.cnblogs.com/stoorz/p/12291540.html
Copyright © 2011-2022 走看看