[codevs1993]草地排水(最大流裸题)

来源:互联网 发布:php供求发布系统源码 编辑:程序博客网 时间:2024/04/29 23:23

传送门
没什么可说的
代码:

#include<cstdio>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<cstdlib>#include<queue>#define ll long longusing namespace std;inline int read(){    int x=0;char ch=' ';int f=1;    while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();    if(ch=='-')f=-1,ch=getchar();    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();    return x*f;}const int inf=0x3f3f3f3f;struct edge{    int to,next,cap,flow;}e[200001];int n,m,s,t,tot=-1;int head[10001];inline void addedge(int x,int y,int l){    e[++tot].to=y;e[tot].next=head[x];e[tot].cap=l;e[tot].flow=0;head[x]=tot;    e[++tot].to=x;e[tot].next=head[y];e[tot].cap=0;e[tot].flow=0;head[y]=tot;}int dis[10001];inline bool bfs(){    memset(dis,-1,sizeof(dis));    queue<int> q;    q.push(s);    dis[s]=0;    while(!q.empty()){        int x=q.front();        q.pop();        for(int i=head[x];i!=-1;i=e[i].next){            int u=e[i].to;            if(dis[u]==-1&&e[i].flow<e[i].cap){                dis[u]=dis[x]+1;                q.push(u);            }        }    }    return dis[t]!=-1;}inline int dfs(int x,int a){    if(x==t||a==0)return a;    int flow=0,f;    for(int i=head[x];i!=-1;i=e[i].next){        int u=e[i].to;        if(e[i].flow<e[i].cap&&dis[u]==dis[x]+1&&(f=dfs(u,min(a,e[i].cap-e[i].flow)))>0){            e[i].flow+=f;            e[i^1].flow-=f;            flow+=f;            a-=f;            if(a==0)break;        }    }    if(!flow)dis[x]=0;    return flow;}inline ll dinic(){    ll flow=0;    while(bfs()){        flow+=dfs(s,inf);    }    return flow;}int main(){    memset(head,-1,sizeof(head));    m=read();n=read();s=1;t=n;    for(int i=1;i<=m;i++){        int x=read(),y=read(),l=read();        addedge(x,y,l);    }    printf("%lld",dinic());    return 0;}
原创粉丝点击