• ZOJ 2314 Reactor Cooling [无源汇上下界网络流]


    贴个板子

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    const int N=205, M=5e5+5, INF=1e9;
    inline ll read(){
    	char c=getchar();ll x=0,f=1;
    	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    	while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    	return x*f;
    }
    
    int n, m, u, v, c, b, s, t;
    int extra[N];
    struct edge{int v, c, f, ne, lower;}e[M];
    int cnt=1, h[N];
    inline void ins(int u, int v, int c, int b=0) { //printf("ins %d %d  %d
    ",u,v,c);
    	e[++cnt]=(edge){v, c, 0, h[u], b}; h[u]=cnt;
    	e[++cnt]=(edge){u, 0, 0, h[v], b}; h[v]=cnt;
    }
    
    int q[N], head, tail, d[N], vis[N], cur[N];
    bool bfs() {
    	memset(vis, 0, sizeof(vis));
    	head=tail=1;
    	q[tail++]=s; d[s]=0; vis[s]=1;
    	while(head!=tail) {
    		int u=q[head++];
    		for(int i=h[u];i;i=e[i].ne) 
    			if(!vis[e[i].v] && e[i].c > e[i].f) {
    				vis[e[i].v]=1; d[e[i].v]=d[u]+1;
    				q[tail++]=e[i].v;
    				if(e[i].v == t) return true;
    			}
    	}
    	return false;
    }
    int dfs(int u, int a) {
    	if(u==t || a==0) return a;
    	int flow=0, f;
    	for(int &i=cur[u];i;i=e[i].ne) 	
    		if(d[e[i].v] == d[u]+1 && (f=dfs(e[i].v, min(e[i].c-e[i].f, a))) >0) {
    			flow += f;
    			e[i].f += f;
    			e[i^1].f -= f;
    			a -= f;
    			if(a==0) break;
    		}
    	if(a) d[u]=-1;
    	return flow;
    }
    int dinic() {
    	int flow=0;
    	while(bfs()) {
    		for(int i=s; i<=t; i++) cur[i]=h[i];
    		flow+=dfs(s, INF);
    	}
    	return flow;
    }
    int main() {
    	freopen("in","r",stdin);
    	int T=read();
    	while(T--) {
    		n=read(); m=read(); s=0; t=n+1;
    		cnt=1; memset(h,0,sizeof(h)); memset(extra, 0, sizeof(extra));
    		for(int i=1; i<=m; i++) 
    			u=read(), v=read(), b=read(), c=read(), ins(u, v, c-b, b), extra[u]-=b, extra[v]+=b;
    		int sum=0;
    		for(int i=1; i<=n; i++) {
    			if(extra[i]>0) ins(s, i, extra[i]), sum+=extra[i];
    			else ins(i, t, -extra[i]);
    		}
    		int flow=dinic(); //printf("flow %d %d
    ",flow,sum);
    		if(flow!=sum) puts("NO");
    		else {
    			puts("YES");
    			for(int i=1; i<=m; i++) printf("%d
    ",e[i<<1].f + e[i<<1].lower);
    		}
    	}
    }
    
    
  • 相关阅读:
    !ps之前一次执行的内容
    shortcut switch in terminal start pos & end pos
    百度词典搜索_dress code
    修杰楷_百度百科
    红米让夏新没活路了,
    绝杀600元以下智能手机的夏新小V二代-专栏-速途网
    李宗瑞_百度百科
    监制_百度百科
    文件上传~Uploadify上传控件
    知方可补不足~CSS中的几个伪元素
  • 原文地址:https://www.cnblogs.com/candy99/p/6629490.html
Copyright © 2020-2023  润新知