洛谷P2472-最大流

题目链接
在这里插入图片描述

分析:

源点 s 连蜥蜴的出生点 容量为INF,每个石柱大于0的点,自建边,容量为初始的高度(限制流量),每个大于0的点 再求下曼哈顿距离能不能到图外,就连下汇点.
模板题啊~~

but: 这么简单的题,我一直WA ,明天等队友来de bug吧!!! 代码先不放了.

。。。。。连接源点的容量应该是1,我nc了 写成INF

AC代码:

#include<bits/stdc++.h>
#define ks ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define fr first
#define sc second
#define pb push_back
#define pf push_front
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>pa;
typedef set<int>::iterator sit;
typedef multiset<int>::iterator msit;
template<class T>inline void read(T &res){
    char c;T flag=1;
    while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
    while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
}
void wenjian(){freopen("concatenation.in","r",stdin);freopen("concatenation.out","w",stdout);}
void tempwj(){freopen("hash.in","r",stdin);freopen("hash.out","w",stdout);}
ll gcd(ll a,ll b){return b == 0 ? a : gcd(b,a % b);}
ll qpow(ll a,ll b,ll mod){a %= mod;ll ans = 1;while(b){if(b & 1)ans = ans * a % mod;a = a * a % mod;b >>= 1;}return ans;}
struct chongzai{int c; bool operator<(const chongzai &b )const{ return c>b.c; } }zai;

const int maxn=5e5+177;
const ll mod=0x3f3f3f3f3f3f3f3f;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;

char mappp[57][57];
int mapp[57][57];
char xy[57][57];
int vism[57][57];


int ma[57][57];
int maa[57][57];


struct Node{
	int next;
	int to;
	int vul;
}edge[maxn];

int head[maxn];
int now;

int tot=0;
int r,c,d;
int xx[maxn],yy[maxn];

int s,t;   // 汇点  源点
int dis[maxn];

queue<int >qu;

pa kk,kk1;

void add(int from,int to,int vul){
	edge[++tot].next=head[from];
	edge[tot].to=to;
	edge[tot].vul=vul;
	head[from]=tot;

	edge[++tot].next=head[to];
	edge[tot].to=from;
	edge[tot].vul=0;
	head[to]=tot;

}
bool check(int x1,int y1,int x2,int y2){

    if(d*d>=((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))){
        return true;
    }
    return false;
}

bool bfs(){
    memset(dis,0,sizeof(dis));
    while(qu.size()){
        qu.pop();
    }
    qu.push(s);
    dis[s]=1;
    while(!qu.empty()){

        int x=qu.front();
        qu.pop();
        for(int i=head[x];i;i=edge[i].next){
            int y=edge[i].to;
            int z=edge[i].vul;
            if(z&&!dis[y]){
                qu.push(y);
                dis[y]=dis[x]+1;
                if(y==t){
                    return true;
                }
            }
        }
    }
    return false;
}


int dinic(int x,int flow){


    if(x==t) return flow;

    int rest=flow,k;
    for(int i=head[x];i&&rest;i=edge[i].next){

        int y=edge[i].to;
        int z=edge[i].vul;
        if(z&&dis[y]==dis[x]+1){

            k=dinic(y,min(rest,z));
            if(!k){
                dis[y]=0;
            }
            edge[i].vul-=k;
            edge[i^1].vul+=k;
            rest-=k;
        }
    }
    return flow-rest;

}


bool checkout(int x,int y){


    if(x-d<1||x+d>r||y-d<1||y+d>c){
        return true;
    }
    return false;

}


void init(){
    tot=1;
    memset(head,0,sizeof(head));
}
int main(){

    init();
    scanf("%d%d%d",&r,&c,&d);
    for(int i=1;i<=r;i++){
        scanf("%s",mappp[i]+1);
    }
    now=1;
    int index=1;

    for(int i=1;i<=r;i++){
        for(int j=1;j<=c;j++){
            mapp[i][j]=mappp[i][j]-'0';
            ma[i][j]=index;
            index++;

            if(mapp[i][j]>=1){
                xx[now]=i;
                yy[now]=j;
                maa[i][j]=now;
                now++;
            }

        }
    }

    for(int i=1;i<=r;i++){
        scanf("%s",xy[i]+1);
    }

    s=r*c*2+1; // 源点
    t=r*c*2+2; // 汇点

    int tmp=r*c;

    for(int i=1;i<now;i++){
        add(i,i+tmp,mapp[xx[i]][yy[i]]);        // 自 联通

        for(int j=1;j<now;j++){                                 // 建边
            if(i==j)continue;
            if(check(xx[i],yy[i],xx[j],yy[j])){
                add(i+tmp,j,INF);
            }
        }
    }

    for(int i=1;i<now;i++){                     // 汇点 和  普通点

        if(checkout(xx[i],yy[i])){
            add(i+tmp,t,INF);
        }

    }
    int maxx=0;

    for(int i=1;i<=r;i++){
        for(int j=1;j<=c;j++){
            if(xy[i][j]=='L'){              // 源点和 出生点建边
                maxx++;
                add(s,maa[i][j],1);
            }
        }
    }
    int flow=0,maxflow=0;

    while(bfs()){
        while(flow=dinic(s,inf)){
            maxflow+=flow;
        }
    }
    printf("%d\n",maxx-maxflow);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值