zoukankan      html  css  js  c++  java
  • 【codevs1993】 草地排水

    http://codevs.cn/problem/1993/ (题目链接)

    题意

      求有向图最大流。

    Solution

      Dinic。

    代码

    // codevs1993
    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<cstdio>
    #include<cmath>
    #include<queue>
    #define LL long long
    #define inf 2147483640
    #define MOD 10000
    #define Pi acos(-1.0)
    #define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
    using namespace std;
     
    const int maxn=210;
    struct edge {int to,next,w;}e[maxn<<1];
    int d[maxn],head[maxn],cnt=1,ans,n,m;
    
    void link(int u,int v,int w) {
    	e[++cnt]=(edge){v,head[u],w};head[u]=cnt;
    	e[++cnt]=(edge){u,head[v],0};head[v]=cnt;
    }
    bool bfs() {
    	memset(d,-1,sizeof(d));
    	queue<int> q;q.push(1);d[1]=0;
    	while (!q.empty()) {
    		int x=q.front();q.pop();
    		for (int i=head[x];i;i=e[i].next) if (e[i].w && d[e[i].to]<0) {
    				d[e[i].to]=d[x]+1;
    				q.push(e[i].to);
    			}
    	}
    	return d[n]>0;
    }
    int dfs(int x,int f) {
    	if (x==n || f==0) return f;
    	int w,used=0;
    	for (int i=head[x];i;i=e[i].next) if (e[i].w && d[e[i].to]==d[x]+1) {
    			w=dfs(e[i].to,min(e[i].w,f-used));
    			used+=w;
    			e[i].w-=w;e[i^1].w+=w;
    			if (used==f) return used;
    		}
    	if (!used) d[x]=-1;
    	return used;
    }
    void Dinic() {
    	while (bfs()) ans+=dfs(1,inf);
    }
    int main() {
    	scanf("%d%d",&m,&n);
    	for (int u,v,w,i=1;i<=m;i++) {
    		scanf("%d%d%d",&u,&v,&w);
    		link(u,v,w);
    	}
    	Dinic();
    	printf("%d",ans);
    	return 0;
    }
    

      

      

  • 相关阅读:
    blocking to nonblocking of Python
    hug -- Embrace the APIs of the future
    supplychain on blockchain
    xstate -- JavaScript state machines and statecharts
    计算PI -- 采用刘徽的割圆术方法
    Gunicorn
    AIOHTTP
    APScheduler
    prefect
    FastAPI
  • 原文地址:https://www.cnblogs.com/MashiroSky/p/6183222.html
Copyright © 2011-2022 走看看