[BZOJ1061][NOI2008]志愿者招募(费用流神题单纯形裸题)

题目描述

传送门

题解

关于费用流的神建图我无言以对。
转自神犇的博客:https://www.byvoid.com/blog/noi-2008-employee/
关于单纯形。。。裸题一道。
大家都用了毕生的经历写关于单纯形模板的解释,而窝坚信一句话:
如果你过几天就忘了,那么你没有真正掌握。——by reflash

代码

费用流

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;

const int max_n=1e3+5;
const int max_m=1e4+5;
const int max_N=max_n+3;
const int max_M=max_N*2+max_m;
const int max_e=max_M*2;
const int INF=1e9;

int n,m,N,x,y,z,maxflow,mincost;
int need[max_n];
int point[max_N],next[max_e],v[max_e],remain[max_e],c[max_e],tot;
int dis[max_N],last[max_N];
bool vis[max_N];
queue <int> q;

inline void addedge(int x,int y,int cap,int z){
    ++tot; next[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=cap; c[tot]=z;
    ++tot; next[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0; c[tot]=-z;
}

inline int addflow(int s,int t){
    int ans=INF,now=t;
    while (now!=s){
        ans=min(ans,remain[last[now]]);
        now=v[last[now]^1];
    }

    now=t;
    while (now!=s){
        remain[last[now]]-=ans;
        remain[last[now]^1]+=ans;
        now=v[last[now]^1];
    }
    return ans;
}

inline bool bfs(int s,int t){
    memset(dis,0x7f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    dis[s]=0;
    vis[s]=true;
    while (!q.empty()) q.pop();
    q.push(s);

    while (!q.empty()){
        int now=q.front(); q.pop();
        vis[now]=false;
        for (int i=point[now];i!=-1;i=next[i])
          if (dis[v[i]]>dis[now]+c[i]&&remain[i]){
            dis[v[i]]=dis[now]+c[i];
            last[v[i]]=i;
            if (!vis[v[i]]){
                vis[v[i]]=true;
                q.push(v[i]);
            }
          }
    }

    if (dis[t]>INF) return false;
    int flow=addflow(s,t);
    maxflow+=flow;
    mincost+=flow*dis[t];
    return true;
}

inline void major(int s,int t){
    maxflow=0; mincost=0;
    while (bfs(s,t));
}

int main(){
    tot=-1;
    memset(point,-1,sizeof(point));
    memset(next,-1,sizeof(next));

    scanf("%d%d",&n,&m);
    N=n+3;
    for (int i=1;i<=n;++i){
        scanf("%d",&need[i]);
        x=need[i]-need[i-1];
        if (x>=0) addedge(1,1+i,x,0);
        else addedge(1+i,N,-x,0);
    }
    x=-need[n];
    if (x>=0) addedge(1,n+2,x,0);
    else addedge(n+2,N,-x,0);
    for (int i=1;i<=m;++i){
        scanf("%d%d%d",&x,&y,&z);
        addedge(1+x,1+y+1,INF,z);
    }
    for (int i=1;i<=n;++i)
      addedge(1+i+1,1+i,INF,0);

    major(1,N);
    printf("%d\n",mincost);
}

单纯形

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;

const int max_n=1005;
const int max_m=10005;
const double eps=1e-7;
const double inf=1e10;

int n,m,L,R;
double A[max_m][max_n],B[max_m],C[max_n],ans;

inline void pivot(int l,int e){
    B[l]/=A[l][e];
    for (int i=1;i<=n;++i)
      if (i!=e)
        A[l][i]/=A[l][e];
    A[l][e]=1/A[l][e];

    for (int i=1;i<=m;++i)
      if (i!=l&&fabs(A[i][e])>eps){
        B[i]-=B[l]*A[i][e];
        for (int j=1;j<=n;++j)
          if (j!=e)
            A[i][j]-=A[l][j]*A[i][e];
        A[i][e]=-A[l][e]*A[i][e];
      }

    ans+=B[l]*C[e];
    for (int i=1;i<=n;++i)
      if (i!=e)
        C[i]-=C[e]*A[l][i];
    C[e]=-C[e]*A[l][e];
}

inline void simplex(){
    int e;
    while (1){
        for (e=1;e<=n;++e)
          if (C[e]>eps) break;
        if (e==n+1) break;

        double data=inf,t; int l;
        for (int i=1;i<=m;++i)
          if (A[i][e]>eps&&(t=B[i]/A[i][e])<data){
            data=t;
            l=i;
          }

        pivot(l,e);
    }
}

int main(){
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;++i)
      scanf("%lf",&C[i]);
    for (int i=1;i<=m;++i){
        scanf("%d%d%lf",&L,&R,&B[i]);
        for (int j=L;j<=R;++j)
          A[i][j]++;
    }
    simplex();
    printf("%0.lf",ans);
}

总结

ORZ hxy神犇——该人曾用费用流艹掉了学长的互测题(标解是单纯形,你们可以根据这道题的建图来脑补一下难度)

你可能感兴趣的:(网络流,单纯形,bzoj,NOI2008)