[USACO4.4]追查坏牛奶Pollutant Control

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
inline int read(){
    char ch=getchar();int sum=0;
    while(ch<'0'||ch>'9') ch=getchar();
    while(ch>='0'&&ch<='9')
        sum*=10,sum+=ch-'0',ch=getchar();
    return sum;
}
#if 0
Writers: G.S.M. && Goes
#endif
const int N=1001000;
const long long A=1001;
const long long INF=(1<<30);
#define ll long long
struct ss{
    ll to,nex;ll cap;
}edge[N<<1];int head[N],ecnt=1;
void add(ll cap,int y,int x){
    edge[++ecnt]=(ss){y,head[x],cap*A+1};
    edge[++ecnt]=(ss){x,head[y],0};
    head[x]=ecnt-1;head[y]=ecnt;
}
#include<queue>
int level[N],n;
bool bfs(){
    memset(level,0,sizeof(level));
    queue<int>Q;Q.push(1);level[1]=1;
    while(!Q.empty()){
        int pos=Q.front();Q.pop();
        for(int i=head[pos];i;i=edge[i].nex)
        if(edge[i].cap&&!level[edge[i].to]){
            level[edge[i].to]=level[pos]+1;
            Q.push(edge[i].to);
        }if(level[n])return true;
    }return level[n];
}
ll dfs(int pos,ll flow){
    if(pos==n||!flow)return flow;int res=0;
    for(int i=head[pos];i;i=edge[i].nex)
    if(edge[i].cap&&level[edge[i].to]==level[pos]+1){
        int tmp=dfs(edge[i].to,min(flow,edge[i].cap));
        edge[i].cap-=tmp;edge[i^1].cap+=tmp;
        flow-=tmp;res+=tmp;if(!flow)return res;
    }return res;
}
ll Dinic(){ll res=0;while(bfs())
    res+=dfs(1,INF);return res;}

int main()
{
    n=read();int m=read();
    while(m--)add(read(),read(),(ll)read());
    ll s=Dinic();printf("%lld %lld",s/A,s%A);
}
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值