zoukankan      html  css  js  c++  java
  • luogu P2711 小行星 |网络流最小割

    题目描述

    星云中有n颗行星,每颗行星的位置是(x,y,z)。每次可以消除一个面(即x,y或z坐标相等)的行星,但是由于时间有限,求消除这些行星的最少次数。

    输入格式

    第1行为小行星个数n,第2行至第n+1行为xi, yi, zi,描述第i个小行星所在的位置。

    输出格式

    共1行,为消除所有行星的最少次数。


    s -> x

    x -> y

    y -> y'

    y' -> z

    z -> t


    #include<queue>
    #include<vector>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int N=4e4+5,M=6*N,inf=1<<30;
    inline int read(){
    	int x=0; char c=getchar();
    	while(c<'0'||c>'9')c=getchar();
    	while(c>='0'&&c<='9'){ x=(x<<1)+(x<<3)+(c^48); c=getchar(); }
    	return x;
    }
    int nxt[M],head[N],go[M],edge[M],tot=1;
    inline void add(int u,int v,int w){
    	nxt[++tot]=head[u],head[u]=tot,go[tot]=v,edge[tot]=w;
    	nxt[++tot]=head[v],head[v]=tot,go[tot]=u,edge[tot]=0;
    }
    int n,m,s,t,d[N];
    inline bool bfs(){
    	memset(d,0,sizeof(d));
    	queue<int>q; q.push(s); d[s]=1;
    	while(q.size()){
    		int u=q.front(); q.pop();
    		for(int i=head[u];i;i=nxt[i]){
    			int v=go[i];
    			if(edge[i]&&!d[v]){
    				d[v]=d[u]+1;
    				q.push(v);
    				if(v==t)return 1;	
    			}
    		}
    	}
    	return 0;
    }
    int dinic(int u,int flow){
    	if(u==t)return flow;
    	int rest=flow;
    	for(int i=head[u];i&&rest;i=nxt[i]){
    		int v=go[i];
    		if(edge[i]&&d[v]==d[u]+1){
    			int k=dinic(v,min(rest,edge[i]));
    			if(!k)d[v]=-1;
    			edge[i]-=k;
    			edge[i^1]+=k;
    			rest-=k;
    		}
    	}
    	return flow-rest;
    }
    signed main(){
    	int n=read();
    	s=0,t=2005;
    	for(int i=1,x,y,z;i<=n;i++){
    		x=read(),y=read(),z=read();
    		add(x,500+y,inf);
    		add(1000+y,1500+z,inf);
    	}
    	for(int i=1;i<=500;i++)add(s,i,1),add(500+i,1000+i,1),add(1500+i,t,1);
    	int maxflow=0,flow=0;
    	while(bfs())while(flow=dinic(s,inf))maxflow+=flow;
    	cout<<maxflow<<endl;
    }
    
  • 相关阅读:
    自定义UIView
    iOS界面-仿网易新闻左侧抽屉式交互
    初级美语 L081:A Heavy Price to Pay 解析
    初级美语 L079:Showing Off 解析
    初级美语 L077:Rome Wasn't Built in a Day 解析
    初级美语 L075:Michael’s Dream 解析
    初级美语 L073:Joe's Restaurant 解析
    初级美语 L071:Working Hard 解析
    初级美语 L069:Running a Bookstore 解析
    初级美语 L067:Getting Married 解析
  • 原文地址:https://www.cnblogs.com/naruto-mzx/p/13061859.html
Copyright © 2011-2022 走看看