裸的最小费用最大流

思路是弄一个原点,弄一个会点,将原点连上物品容量为c[i],

费用为零,将物品和人按a数组的关系连起来,容量为INF,花费为零,然后将人和会点连起来容量为他

做几件物品,也就是ss[j]-ss[j-1],花费为他做这几件的愤怒值,再跑最大流最小费用算法就行了;

#include<cstdio>
#include<iostream>
#include<cstring> 
#include<queue>
#define LL long long
using namespace std;
const int M=199999;
const int INF=2139062143;
int n,m,s,t;int dis[M],c[M],a[999][999],ss[M];
int nex[M],head[M],cos[M],to[M],tot,pre[M],cap[M],vis[M],flo[M],id[M];
int add(int x,int y,int z,int w){
    cos[tot]=z;
    cap[tot]=w;
    nex[++tot]=head[x];
    to[tot]=y;
    head[x]=tot;
     cos[tot]=-z;
    cap[tot]=0;
    nex[++tot]=head[y];
    to[tot]=x;
    head[y]=tot;
}
int spfa(int s,int t){
    queue <int> q;
    memset(dis,127,sizeof(dis));
    memset(vis,0,sizeof(vis));
    memset(pre,-1,sizeof(pre));
    dis[s]=0;
    vis[s]=1;
    q.push(s);
    flo[s]=INF;
    while(!q.empty()){
        int x=q.front();
        vis[x]=0;
        q.pop();
        for(int i=head[x];i;i=nex[i])
        {
            int tmp=to[i];
            if(dis[tmp]>dis[x]+cos[i-1]&&cap[i-1]){
                dis[tmp]=dis[x]+cos[i-1];
                pre[tmp]=x;
                flo[tmp]=min(flo[x],cap[i-1]);
                id[tmp]=i-1;
                if(!vis[tmp]){
                q.push(tmp);
                vis[tmp]=1;
                }
            }
        }
    }
    if(dis[t]>=INF)return 0;
    return 1; 
}
struct st{
    LL cost;
    LL flow;
}; 
st  maxflow(int s,int t){
       st a;
       a.flow=0,a.cost=0;
    while(spfa(s,t)){
        int k=t;
           while(k!=s){
            cap[id[k]]-=flo[t];
            cap[id[k]^1]+=flo[t];
            k=pre[k];
        }
           //a.flow+=flo[t];
           a.cost+=flo[t]*dis[t];
    }
    return a;
}
int main(){
    scanf("%d%d",&m,&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&c[i]);
        add(0,i,0,c[i]);
    }// 将原点连上物品容量为c[i],费用为零
    for(int i=1;i<=m;i++)
    for(int j=1;j<=n;j++){
        scanf("%d",&a[i][j]);
        if(a[i][j]){
            add(j,i+n,0,INF);//将物品和人按a数组的关系连起来,容量为INF,花费为零
        }
    }
    for(int i=1;i<=m;i++){
        int s;
        scanf("%d",&s);
        for(int j=1;j<=s;j++){
            scanf("%d",&ss[j]);
        } 
        ss[s+1]=INF;
        for(int j=1;j<=s+1;j++){
            int w;
            scanf("%d",&w);
                add(i+n,n+m+1,w,ss[j]-ss[j-1]);//然后将人和会点连起来容量为他做几件物品,也就是ss[j]-ss[j-1],花费为他做这几件的愤怒值
        }
    }
    st d=maxflow(0,n+m+1);
    printf("%lld",d.cost);
    return 0;
}