网络流例题-小行星二维到三维

今天写了两道网络流的经典例题,总结一下两道题的做法。
宣传一波个人博客

二维小行星 POJ3041

题目大意:

给你 n n n个小行星的坐标 ( x , y ) (x,y) (x,y) ,$ 1 \leq n \leq 500 ,x,y \leq n$

你每次可以消除某一行或某一列的小行星,求最少消除次数。

题解

把每个 x , y x,y x,y分开做节点,对于每一个小行星 ( x , y ) (x,y) (x,y),把 x x x节点和 y y y节点连一条边,那么题目

变成了要我们求最小点覆盖了,那么可以直接转化成网络流来做。

/*******************************
Author:galaxy yr
LANG:C++
Created Time:2019年09月28日 星期六 21时35分41秒
*******************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>

using namespace std;

struct edge{
    int to,next,w;
};

const int maxn=505;
const int inf=0x7fffffff;
int n,k,head[maxn*3],cnt,s,t,depth[maxn*3],dis[maxn*3];
edge e[maxn*maxn];

void add(int u,int v,int w)
{
    e[cnt].to=v;
    e[cnt].next=head[u];
    e[cnt].w=w;
    head[u]=cnt++;
}

bool bfs()
{
    memset(depth,-1,sizeof depth);depth[s]=0;
    std::queue<int>q;q.push(s);
    while(!q.empty())
    {
        int u=q.front();q.pop();
        for(int i=head[u];i!=-1;i=e[i].next)
            if(depth[e[i].to]==-1 && e[i].w>0)
            {
                depth[e[i].to]=depth[u]+1;
                q.push(e[i].to);
            }
    }
    return depth[t]!=-1;
}

int dfs(int u,int dist)
{
    if(u==t)return dist;
    for(int i=head[u];i!=-1;i=e[i].next)
        if(depth[e[i].to]==depth[u]+1 && e[i].w>0)
        {
            int d=dfs(e[i].to,min(dist,e[i].w));
            if(d>0)
            {
                e[i].w-=d;
                e[i^1].w+=d;
                return d;
            }
        }
    return 0;
}

int Dinic()
{
    int ans=0;
    while(bfs())
        while(int d=dfs(s,inf))
            ans+=d;
    return ans;
}

int main()
{
    freopen("poj3047.in","r",stdin);
    freopen("poj3047.out","w",stdout);
    memset(head,-1,sizeof head);
    cin>>n>>k;
    s=n+n+1;t=n+n+2;
    for(int i=1;i<=n;i++)
    {
        add(s,i,1);
        add(i,s,0);
        add(i+n,t,1);
        add(t,i+n,0);
    }
    int u,v;
    for(int i=1;i<=k;i++)
    {
        cin>>u>>v;
        add(u,v+n,1);
        add(v+n,u,0);
    }
    cout<<Dinic()<<endl;
    return 0;
}

升级为三维 洛谷 P2711

题目大意:

给你 n n n个小行星的坐标 ( x , y , z ) (x,y,z) (x,y,z) ,$ 1 \leq n \leq 50000 ,1 \leq x,y \leq 500$

你每次可以消除某一个平面的小行星,求最少消除次数。

题解

考虑把 x , y , z x,y,z x,y,z分开做节点,然后对于每一个小行星 ( x , y , z ) (x,y,z) (x,y,z)坐标之间连边,那么消除一个平面即

为断一条边的代价,题目即要求我们求最小割。(连边还是很简单的,可以自己想一想)

/*******************************
Author:galaxy yr
LANG:C++
Created Time:2019年09月29日 星期日 10时31分05秒
*******************************/
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>

using namespace std;

struct edge{
    int to,next,w;
    edge(int a=0,int b=0,int c=0):to(a),next(b),w(c){}
};

const int maxn=505;
const int maxm=50005;
const int N=500;
const int _2N=1000;
const int _3N=1500;
const int inf=0x7fffffff;
int n,head[maxn*5],s=_3N+N+1,t=_3N+N+2,depth[maxn*5],cnt;
edge e[maxm*10];

void add(int u,int v,int w)
{
    e[cnt]=edge(v,head[u],w);
    head[u]=cnt++;
}

void Add(int u,int v,int w)
{
    add(u,v,w);
    add(v,u,0);
}

bool bfs()
{
    memset(depth,-1,sizeof depth); depth[s]=0;
    queue<int>que; que.push(s);
    while(!que.empty())
    {
        int u=que.front(); que.pop();
        for(int i=head[u];i!=-1;i=e[i].next)
            if(e[i].w>0 && depth[e[i].to]==-1)
            {
                depth[e[i].to]=depth[u]+1;
                que.push(e[i].to);
            }
    }
    return (depth[t]>=0);
}

int dfs(int now,int dist)
{
    if(now==t)return dist;
    for(int i=head[now];i!=-1;i=e[i].next)
        if(e[i].w>0 && depth[e[i].to]==depth[now]+1)
        {
            int d=dfs(e[i].to,min(dist,e[i].w));
            if(d>0)
            {
                e[i].w-=d;
                e[i^1].w+=d;
                return d;
            }
        }
    return 0;
}

int Dinic()
{
    int ans=0;
    while(bfs())
        while(int d=dfs(s,inf))
            ans+=d;
    return ans;
}

int main()
{
    freopen("p2711.in","r",stdin);
    freopen("p2711.out","w",stdout);
    memset(head,-1,sizeof head);
    cin>>n;
    int x,y,z;
    for(int i=1;i<=n;i++)
    {
        cin>>x>>y>>z;
        Add(x,y+N,inf);
        Add(y+N,y+_2N,1);
        Add(y+_2N,z+_3N,inf);
    }
    for(int i=1;i<=N;i++)
    {
        Add(s,i,1);
        Add(i+_3N,t,1);
    }
    cout<<Dinic()<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值