【题目分析】

无源汇上下界可行流。

上下界网络流的问题可以参考这里。↓

http://www.cnblogs.com/kane0526/archive/2013/04/05/3001108.html

【代码】

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>

//#include <map>
#include <set>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>

using namespace std;

#define maxn 205
#define me 50005
#define inf 0x3f3f3f3f
#define F(i,j,k) for (int i=j;i<=k;++i)
#define D(i,j,k) for (int i=j;i>=k;--i)

void Finout()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    #endif
}

int Getint()
{
    int x=0,f=1; char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();}
    while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}

int h[me<<1],to[me<<1],ne[me<<1],fl[me<<1],en=0,S=0,T=me-1;
int id[me<<1];

void add(int a,int b,int c,int ID)
{
//	cout<<"add "<<a<<" "<<b<<" "<<c<<endl;
    to[en]=b; ne[en]=h[a]; fl[en]=c; id[en]=ID; h[a]=en++;
    to[en]=a; ne[en]=h[b]; fl[en]=0; id[en]=0; h[b]=en++;
}

int map[me];

bool tell()
{
    queue <int> q;
    memset(map,-1,sizeof map);
    map[S]=0;
    while (!q.empty()) q.pop();
    q.push(S);
    while (!q.empty())
    {
        int x=q.front(); q.pop();
//        cout<<"bfs"<<x<<endl;
        for (int i=h[x];i>=0;i=ne[i])
        {
//        	cout<<"to "<<to[i]<<endl;
            if (map[to[i]]==-1&&fl[i]>0)
            {
                map[to[i]]=map[x]+1;
                q.push(to[i]);
            }
        }
    }
//    cout<<"over"<<endl;
    if (map[T]!=-1) return true;
    return false;
}

int zeng(int k,int r)
{
    if (k==T) return r;
    int ret=0;
    for (int i=h[k];i>=0&&ret<r;i=ne[i])
        if (map[to[i]]==map[k]+1&&fl[i]>0)
        {
            int tmp=zeng(to[i],min(fl[i],r-ret));
            ret+=tmp; fl[i]-=tmp; fl[i^1]+=tmp;
        }
    if (!ret) map[k]=-1;
    return ret;
}

int ans[me<<1],n,m,du[me<<1],dn[me<<1];

int main()
{
    Finout();
    while (scanf("%d%d",&n,&m)!=EOF)
    {
    	memset(h,-1,sizeof h);
    	memset(ans,0,sizeof ans);
    	memset(du,0,sizeof du);
    	memset(dn,0,sizeof dn);
    	F(i,1,m)
    	{
    		int a,b,c,d;
    		a=Getint();b=Getint();c=Getint();d=Getint();
    		add(a,b,d-c,i);
    		du[a]-=c; du[b]+=c; dn[i]+=c;
		}
		F(i,1,n)
		{
			if (du[i]) add(S,i,du[i],0);
			if (du[i]<0) add(i,T,-du[i],0);
		}
		int now=0,tmp=0;
		while (tell()) while (tmp=zeng(S,inf)) now+=tmp;
		int flag=1;
		for (int i=h[S];i>=0;i=ne[i])
			if (fl[i]>0) flag=0;
		if (!flag) printf("NO\n");
		else
		{
			printf("YES\n");
			F(i,0,en-1) ans[id[i]]=fl[i^1];
			F(i,1,m) printf("%d\n",ans[i]+dn[i]);
		}
	}
}

  

SGU 194 Reactor Cooling ——网络流的更多相关文章

  1. SGU 194 Reactor Cooling (无源上下界网络流)

    The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear ...

  2. SGU 194 Reactor Cooling 无源汇带上下界可行流

    Reactor Cooling time limit per test: 0.5 sec. memory limit per test: 65536 KB input: standard output ...

  3. SGU 194 Reactor Cooling(无源无汇上下界可行流)

    Description The terrorist group leaded by a well known international terrorist Ben Bladen is bulidin ...

  4. SGU 194. Reactor Cooling(无源汇有上下界的网络流)

    时间限制:0.5s 空间限制:6M 题意: 显然就是求一个无源汇有上下界的网络流的可行流的问题 Solution: 没什么好说的,直接判定可行流,输出就好了 code /* 无汇源有上下界的网络流 * ...

  5. 【无源汇上下界最大流】SGU 194 Reactor Cooling

    题目链接: http://acm.sgu.ru/problem.php?contest=0&problem=194 题目大意: n个点(n<20000!!!不是200!!!RE了无数次) ...

  6. SGU 194 Reactor Cooling

    http://acm.sgu.ru/problem.php?contest=0&problem=194 题意:m条有向边,有上下界,求最大流. 思路:原图中有u-v low[i],high[i ...

  7. SGU 194 Reactor Cooling Dinic求解 无源无汇有上下界的可行流

    题目链接 题意:有向图中有n(1 <= n <= 200)个点,无自环或者环的节点个数至少为3.给定每条边的最小流量和最大流量,问每条边的可行流量为多少? 思路:一般求解的网络流并不考虑下 ...

  8. sgu 194 Reactor Cooling(有容量上下界的无源无汇可行流)

    [题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20757 [题意] 求有容量上下界的无源无汇可行流. [思路] ...

  9. SGU 194 Reactor Cooling (有容量和下界的可行流)

    题意:给定上一个有容量和下界的网络,让你求出一组可行解. 析:先建立一个超级源点 s 和汇点 t ,然后在输入时记录到每个结点的下界的和,建边的时候就建立c - b的最后再建立 s 和 t , 在建立 ...

随机推荐

  1. ImageLoader的Jar包加载图片

    注意:需要添加权限,若缓存图片第二个必须要加 <uses-permission android:name="android.permission.INTERNET" /> ...

  2. 对于IE6版本图片透明。

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. html 7.28

    1.HTML标记在没有添加任何样式的时候,默认是有间距的,所以我们要先把把所有标记归置内(padding)外(margin)边距归置为0 2.HTML5 中不支持 <acronym> 标签 ...

  4. socket通信的json数据传输与获取

    本文是基于scoket通信的tcp来进行数据的json格式传输与获取的. 首先,我们先要下载AsyncSockethttps://github.com/robbiehanson/CocoaAsyncS ...

  5. JNDI深入浅出

    1.什么是JNDI JNDI(The Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API.命名服务将名称和 ...

  6. NYOJ 925 国王的烦恼

    从最后一天开始往前加边. 同一天的边同时加到图上,加完之后检查集合数量是否和没加之前有变化. 有变化的话,答案就+1. #include<cstdio> #include <iost ...

  7. CodeForces 190D Non-Secret Cypher

    双指针. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...

  8. hdu_5711_Ingress(TSP+贪心)

    题目连接:hdu5711 这题是 HDU 女生赛最后一题,TSP+贪心,确实不好想,看了wkc巨巨的题解,然后再做的 题解传送门:Ingress #include<cstdio> #inc ...

  9. Ubuntu将新增磁盘挂载到home下

    home磁盘空间不足,其他闲置硬盘是原来windows的,不能直接使用(磁盘格式及权限等原因),比如编译安卓源码等. 这样的话就需要将新的磁盘格式化成fat32后挂载到/home下的一个目录,这样就可 ...

  10. emacs command

    eval-buffer用来执行.emacs不要再重启了,或cxce执行光标前的一行 eval-region load-file ~/.emacs goto-line global-set-key定义快 ...