BZOJ 2039 人员雇佣(最小割)

最小割的建图模式一般是,先算出总收益,然后再通过网络模型进行割边减去部分权值。

然后我们需要思考什么才能带来收益,什么才能有权值冲突。

s连向选的点,t连向不选的点,那么收益的减少量应该就是将s集和t集分开的割边集。

下面说这道题的建图:

点:

  每个人一个点,额外设源汇点。

边:

  源向人连这个人能造成的全部收益(当作雇佣所有人,然后此人造成的收益)

  人与人之间连两人熟悉度*2,呃,题意问题。

  人向汇连雇佣需要花的钱。

 

# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <bitset>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi acos(-1.0)
# define eps 1e-8
# define MOD 1000000007
# define INF (LL)1<<60
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<1,l,mid
# define rch p<<1|1,mid+1,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
    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;
}
const int N=1005;
//Code begin...

struct Edge{int p, next; LL w;}edge[N*N*5];
int head[N], cnt=2, s, t, vis[N];
queue<int>Q;
LL ss[N];

void add_edge(int u, int v, LL w){
    edge[cnt].p=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++;
    edge[cnt].p=u; edge[cnt].w=0; edge[cnt].next=head[v]; head[v]=cnt++;
}
int bfs(){
    int i, v;
    mem(vis,-1); vis[s]=0; Q.push(s);
    while (!Q.empty()) {
        v=Q.front(); Q.pop();
        for (int i=head[v]; i; i=edge[i].next) {
            if (edge[i].w>0&&vis[edge[i].p]==-1) vis[edge[i].p]=vis[v]+1, Q.push(edge[i].p);
        }
    }
    return vis[t]!=-1;
}
LL dfs(int x, LL low){
    int i;
    LL a, temp=low;
    if (x==t) return low;
    for (int i=head[x]; i; i=edge[i].next) {
        if (edge[i].w>0&&vis[edge[i].p]==vis[x]+1) {
            a=dfs(edge[i].p,min(edge[i].w,temp));
            temp-=a; edge[i].w-=a; edge[i^1].w+=a;
            if (temp==0) break;
        }
    }
    if (temp==low) vis[x]=-1;
    return low-temp;
}
LL dinic(){
    LL sum=0;
    while (bfs()) sum+=dfs(s,INF);
    return sum;
}
int main ()
{
    int n;
    LL ans=0, x;
    scanf("%d",&n); s=0; t=n+1;
    FOR(i,1,n) scanf("%lld",&x), add_edge(i,t,x);
    FOR(i,1,n) FOR(j,1,n) {
        scanf("%lld",&x); ss[i]+=x;
        if (i==j||!x) continue;
        add_edge(i,j,x*2);
    }
    FOR(i,1,n) add_edge(s,i,ss[i]), ans+=ss[i];
    LL res=dinic();
    printf("%lld\n",ans-res);
    return 0;
}
View Code

 

posted @ 2017-05-20 12:40  free-loop  阅读(149)  评论(0编辑  收藏  举报