zoukankan      html  css  js  c++  java
  • P3980-[NOI2008]志愿者招募【费用流】

    正题

    题目链接:https://www.luogu.com.cn/problem/P3980


    题目大意

    (n)天,第(i)天需要(A_i)个志愿者。有(m)种志愿者,第(i)种从(s_i)天服务到(t_i)天,需要(c_i)元的费用。

    求满足条件的最小费用

    (1leq nleq 1000,1leq mleq 10000)


    解题思路

    考虑费用流

    如果雇佣了(s_i)天到(t_i)天的话那么就相当于将这段范围(A_i)的值减一,注意到是区间的(1)需要(c_i)的费用,那么肯定这个条件是压缩成一条边的,也就是(s_i)(t_i+1)连接费用为(1)的边。

    这样的话考虑如何满足条件,注意到是减一也就是抽走一条经过(s_i)(t_i)的流量,也就是对于这些流量的限制。

    建立(n)个点,(i)(i+1)连接流量为(T-A_i)(T)是一个很大的数就可以了)表示至少需要抽走(A_i)的流量就好了。

    然后跑费用流


    code

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #define ll long long
    using namespace std;
    const ll N=1100,T=(1ll<<31),inf=1e18;
    struct node{
    	ll to,next,w,c;
    }a[N*22];
    ll n,m,s,t,tot=1,ans,ls[N],f[N],mf[N],pre[N];
    bool v[N];queue<int> q;
    void addl(ll x,ll y,ll w,ll c){
    	a[++tot].to=y;a[tot].next=ls[x];ls[x]=tot;a[tot].w=w;a[tot].c=c;
    	a[++tot].to=x;a[tot].next=ls[y];ls[y]=tot;a[tot].w=0;a[tot].c=-c;
    	return;
    }
    bool SPFA(){
    	memset(f,0x3f,sizeof(f));
    	q.push(s);f[s]=0;v[s]=1;mf[s]=T;
    	while(!q.empty()){
    		ll x=q.front();q.pop();v[x]=0;
    		for(ll i=ls[x];i;i=a[i].next){
    			ll y=a[i].to;
    			if(a[i].w&&f[x]+a[i].c<f[y]){
    				f[y]=f[x]+a[i].c;pre[y]=i;
    				mf[y]=min(mf[x],a[i].w);
    				if(!v[y])v[y]=1,q.push(y);
    			}
    		}
    	}
    	return (f[t]<inf);
    }
    void Updata(){
    	ll x=t;ans+=mf[t]*f[t];
    	while(x!=s){
    		a[pre[x]].w-=mf[t];
    		a[pre[x]^1].w+=mf[t];
    		x=a[pre[x]^1].to;
    	}
    	return;
    }
    signed main()
    {
    	scanf("%lld%lld",&n,&m);
    	s=n+2;t=s+1;
    	addl(s,1,T,0);
    	for(ll i=1;i<=n;i++){
    		ll x;
    		scanf("%lld",&x);
    		addl(i,i+1,T-x,0);
    	}
    	for(ll i=1;i<=m;i++){
    		ll s,t,c;
    		scanf("%lld%lld%lld",&s,&t,&c);
    		addl(s,t+1,T,c);
    	}
    	addl(n+1,t,T,0);
    	while(SPFA())
    		Updata();
    	printf("%lld
    ",ans);
    	return 0;
    }
    
  • 相关阅读:
    English trip V1
    English trip M1
    every day a practice —— morning(5)
    English Voice of <<All Of Me>>
    bzoj 3561 DZY Loves Math VI
    luogu P4322 [JSOI2016]最佳团体
    luogu P3264 [JLOI2015]管道连接
    bzoj 5084 hashit
    luogu P6091 原根
    bzoj 5206 [Jsoi2017]原力
  • 原文地址:https://www.cnblogs.com/QuantAsk/p/15021627.html
Copyright © 2011-2022 走看看