zoukankan      html  css  js  c++  java
  • [bzoj3144] [HNOI2013]切糕

    Description

    img

    Input

    第一行是三个正整数P,Q,R,表示切糕的长P、 宽Q、高R。第二行有一个非负整数D,表示光滑性要求。接下来是R个P行Q列的矩阵,第z个 矩阵的第x行第y列是v(x,y,z) (1≤x≤P, 1≤y≤Q, 1≤z≤R)。
    100%的数据满足P,Q,R≤40,0≤D≤R,且给出的所有的不和谐值不超过1000。

    Output

    仅包含一个整数,表示在合法基础上最小的总不和谐值。

    Sample Input

    2 2 2                          
    1 
    6 1
    6 1
    2 6
    2 6
    

    Sample Output

    6
    

    Solution

    最小割(其实题目长得就很像最小割)

    考虑没有限制,可以看做是个最小割模型,即有(pcdot q)条链,每条链割一条边,总代价最小。

    那么加上限制之后,对于点((i,j,k))(其中(i)是高),向((i-h,j,k))连一条边权为(inf)的边,即可。

    这里是因为如果相邻两点割的高度超过(h),这条(inf)的边还是将上下连起来了。

    然后就做完了。

    注意代码里注释了的那个剪枝,超级快!去掉两分钟都跑不出来。

    #include<bits/stdc++.h>
    using namespace std;
    
    //#define ONLINE_JUDGE
    
    #ifdef ONLINE_JUDGE
    #define getchar() ((p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin)),p1==p2)?EOF:*p1++)
    #endif
    
    namespace fast_IO {
    	char buf[1<<21],*p1=buf,*p2=buf;
    
    	template <typename T> inline void read(T &x) {
    		x=0;T f=1;char ch=getchar();
    		for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
    		for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
    	}
    	template <typename T,typename... Args> inline void read(T& x,Args& ...args) {
    		read(x),read(args...);
    	}
    
    	char buf2[1<<21],a[80];int p,p3=-1;
    
    	inline void flush() {fwrite(buf2,1,p3+1,stdout),p3=-1;}
    	template <typename T> inline void write(T x) {
    		if(p3>(1<<20)) flush();
    		if(x<0) buf2[++p3]='-',x=-x;
    		do {a[++p]=x%10+48;} while(x/=10);
    		do {buf2[++p3]=a[p];} while(--p);
    		buf2[++p3]='
    ';
    	}
    	template <typename T,typename... Args> inline void write(T x,Args ...args) {
    		write(x),write(args...);
    	}
    }
    
    using fast_IO :: read;
    using fast_IO :: write;
    using fast_IO :: flush;
    
    const int maxn = 1e5+10;
    const int inf = 1e9;
    
    int p,q,r,h,head[maxn],tot=1,s,t,max_flow,vis[maxn],dis[maxn];
    struct edge{int to,nxt,w;}e[maxn<<1];
    
    void add(int u,int v,int w) {e[++tot]=(edge){v,head[u],w},head[u]=tot;}
    void ins(int u,int v,int w) {add(u,v,w),add(v,u,0);}
    
    int bfs() {
    	memset(vis,0,sizeof vis);
    	memset(dis,-1,sizeof dis);
    	queue<int > que;que.push(s),vis[s]=1,dis[s]=0;
    	while(!que.empty()) {
    		int now=que.front();que.pop();
    		for(int i=head[now];i;i=e[i].nxt)
    			if(e[i].w&&dis[e[i].to]==-1) 
    				dis[e[i].to]=dis[now]+1,que.push(e[i].to);
    	}return dis[t]!=-1;
    }
    
    int dfs(int x,int f) {
    	if(x==t) return max_flow+=f,vis[t]=1,f;
    	vis[x]=1;int used=0;
    	for(int i=head[x];i;i=e[i].nxt)
    		if(e[i].w&&dis[e[i].to]==dis[x]+1) {
    			int d=dfs(e[i].to,min(f-used,e[i].w));
    			if(d>0) e[i].w-=d,e[i^1].w+=d,used+=d;
    			if(used==f) break;
    		}
    	if(!used) dis[x]=-1;   //神仙优化!!!!
    	return used;
    }
    
    void dinic() {
    	while(bfs()) {vis[t]=1;while(vis[t]) memset(vis,0,sizeof vis),dfs(s,inf);}
    }
    
    const int dx[5]={0,1,-1,0,0};
    const int dy[5]={0,0,0,1,-1};
    
    int calc(int i,int j,int k) {return (i-1)*p*q+(j-1)*q+k;}
    int check(int i,int j,int k) {return i+dx[k]<=p&&i+dx[k]>=1&&j+dy[k]<=q&&j+dy[k]>=1;}
    
    int main() {
    	read(p,q,r,h);int x;
    	for(int i=1;i<=r;i++)
    		for(int j=1;j<=p;j++)
    			for(int k=1;k<=q;k++) 
    				read(x),ins(calc(i,j,k),calc(i+1,j,k),x);
    	for(int i=h+1;i<=r;i++)
    		for(int j=1;j<=p;j++)
    			for(int k=1;k<=q;k++)
    				for(int d=1;d<=4;d++)
    					if(check(j,k,d)) 
    						ins(calc(i,j,k),calc(i-h,j+dx[d],k+dy[d]),inf);
    	s=0,t=(r+1)*p*q+1;
    	for(int i=1;i<=p;i++)
    		for(int j=1;j<=q;j++)
    			ins(s,calc(1,i,j),inf),ins(calc(r+1,i,j),t,inf);
    	dinic();write(max_flow);
    	flush();
    	return 0;
    }
    
  • 相关阅读:
    生成器
    迭代器
    装饰器
    Maven工具学习(六)----Maven依赖的版本锁定与版本常量
    SpringBoot学习记录之整合JSP
    SpringBoot学习记录之入门篇
    【k8s】ep-addresses
    【k8s】ep-metadata
    【k8s】Endpoints
    【k8s】cj-suspend
  • 原文地址:https://www.cnblogs.com/hbyer/p/10199776.html
Copyright © 2011-2022 走看看