首页 技术 正文
技术 2022年11月9日
0 收藏 318 点赞 4,151 浏览 2025 个字

【题目分析】

无源汇上下界可行流。

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

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]);}}}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,910
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,497
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,135
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298