BZOJ_P3265 志愿者招募加强版(最小费用最大流)

48 篇文章 0 订阅
23 篇文章 0 订阅

BZOJ传送门
Time Limit: 20 Sec Memory Limit: 512 MB
Submit: 332 Solved: 164
[Submit][Status][Discuss]
Description
这里写图片描述

Input
这里写图片描述

Output
这里写图片描述

Sample Input
3 3
2 3 4
1 1 2 2
1 2 3 5
1 3 3 2

Sample Output
14

HINT
这里写图片描述

Source
单纯形

不会单纯形,跑网络流
在BZOJ居然Rank1,截图纪念一下QuQ
这里写图片描述

#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<iostream>
using namespace std;
#define N 1050
#define U 100000
#define INF 2139062143
inline int in(int x=0,int v=1,char ch=getchar()){
    while(ch!='-'&&(ch>'9'||ch<'0')) ch=getchar();if(ch=='-') v=-1,ch=getchar();
    while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x*v;
}
struct NetWork{
    struct Edge{int fr,to,cap,flow,cost;}edge[N*900];vector<int> g[N];
    int n,s,t,m,b[N],d[N],a[N],p[N],cost,cnt,v[N][2];

    inline void Add_Edge(int fr,int to,int flow,int cost){
        edge[cnt++]=((Edge){fr,to,flow,0,cost});
        edge[cnt++]=((Edge){to,fr,0,0,-cost});
        g[fr].push_back(cnt-2);g[to].push_back(cnt-1);
    }
    inline int spfa(){
        queue<int> q;bool b[N];int x,l;
        memset(b,0,sizeof(b));memset(d,0x7f,sizeof(d));
        b[s]=1,d[s]=0,a[s]=INF;q.push(s);
        while(!q.empty()){
            x=q.front();q.pop();b[x]=false;l=g[x].size();
            for(int i=0;i<l;i++){
                Edge& e=edge[g[x][i]];
                if(e.cap>e.flow&&d[e.to]>d[x]+e.cost){
                    d[e.to]=d[x]+e.cost;
                    p[e.to]=g[x][i];
                    a[e.to]=min(a[x],e.cap-e.flow);
                    if(!b[e.to]){b[e.to]=1;q.push(e.to);}
                }
            }
        }
        if(d[t]==INF) return 0;
        cost+=d[t]*a[t];
        for(x=t;x!=s;x=edge[p[x]].fr) edge[p[x]].flow+=a[t],edge[p[x]^1].flow-=a[t];
        return 1;
    }
    void init(){
        n=in(),m=in();int x,y,z;s=0,t=n+1;
        Add_Edge(s,1,U,0);
        for(int i=1;i<=n;i++) x=in(),Add_Edge(i,i+1,U-x,0);
        for(int i=1;i<=m;i++){
            x=in();
            for(int j=1;j<=x;j++) v[j][0]=in(),v[j][1]=in();
            y=in();
            for(int j=1;j<=x;j++) Add_Edge(v[j][0],v[j][1]+1,INF,y);
        }
        while(spfa());
        printf("%d\n",cost);
    }
}s;
int main(){s.init();return 0;}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值