首页 > 代码库 > bzoj4873

bzoj4873

http://www.lydsy.com/JudgeOnline/problem.php?id=4873

最大权闭合子图。。。

建图:

1.d[i][j]:i->j区间的费用,d[i][j] > 0 ins(S,id(i,j),d[i][j]) 否则ins(id(i,j),T,-d[i][j]) 套路

2.对于寿司怎么搞,m=1,ins(种类,T,a[i]*a[i]),ins(寿司,种类,inf):必须割掉初始的费用,ins(寿司,T,a[i]),ins(区间,寿司, inf):每个区间割掉需要寿司的花费

3.ins(id(i,j),id(i+1,j),inf),ins(id(i,j),id(i,j-1),inf):选了大的区间的必须选小的区间

但是上面有一步可以改进,就是2的最后。因为选了大的一定会选小的,那么我们只用将[i,i]这个区间向寿司连边就行了。

记住a[i]有1000,并且汇点不要取太小。。。

技术分享
#include<bits/stdc++.h>
using namespace std;
const int N = 100010, inf = 1 << 29;    
struct edge {
    int nxt, to, f;
} e[N * 2];
int dis[N], used[N], head[N], q[N], iter[N], d[110][110], a[N];
int n, m, sum, T = 0, num = 0, cnt = 1;
namespace maxflow
{
    void link(int u, int v, int f)
    {
        e[++cnt].nxt = head[u];
        head[u] = cnt;
        e[cnt].to = v;
        e[cnt].f = f;
    }
    void ins(int u, int v, int f)
    {
        link(u, v, f); link(v, u, 0);
    }
    bool bfs()
    {
        int l = 1, r = 0; q[++r] = 0;
        memset(dis, 0, sizeof(dis)); dis[0] = 1;
        while(l <= r)
        {
            int u = q[l++];
            for(int i = head[u]; i; i = e[i].nxt) if(!dis[e[i].to] && e[i].f)
            {
                dis[e[i].to] = dis[u] + 1;
                q[++r] = e[i].to;
            }
        }
        return dis[T] > 0;
    }
    int dfs(int u, int delta)
    {
        if(u == T) return delta;
        int ret = 0;
        for(int &i = iter[u]; i && delta; i = e[i].nxt) if(e[i].f && dis[e[i].to] == dis[u] + 1)
        {
            int x = dfs(e[i].to, min(delta, e[i].f));
            e[i].f -= x; e[i ^ 1].f += x;
            ret += x; delta -= x;
        }
        return ret;
    }
    int id(int i, int j) { return (i - 1) * n + j; }
    void build()
    {
        //每个编号和T连边,每个寿司和对应编号连边 
        int D = n * n;
        T = N - 2;
        for(int i = 1; i <= n; ++i)
        { // i + D:寿司 a[i] + 2 * D: 种类 id(i, i): 区间 
            ins(i + D, T, a[i]); //每个寿司 
            if(d[i][i] < 0) ins(id(i, i), T, -d[i][i]);
            else ins(0, id(i, i), d[i][i]);
            ins(id(i, i), i + D, inf);
            if(!m) continue;
            if(!used[a[i]])
            {
                used[a[i]] = 1;
                ins(a[i] + 2 * D, T, a[i] * a[i]);
            }
            ins(i + D, a[i] + 2 * D, inf);                    
        } 
        for(int i = 1; i <= n; ++i)    
            for(int j = i + 1; j <= n; ++j)
            {
                if(d[i][j] < 0) ins(id(i, j), T, -d[i][j]);
                else ins(0, id(i, j), d[i][j]);  
                if(i < n) ins(id(i, j), id(i + 1, j), inf);
                if(j > 1) ins(id(i, j), id(i, j - 1), inf);
            } 
    }
    int dinic()
    {
        int ret = 0;
        while(bfs())
        {
            for(int i = 0; i <= T; ++i) iter[i] = head[i];
            ret += dfs(0, inf);
        }
        return ret;
    }
} using namespace maxflow;
int main()
{
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
    for(int i = 1; i <= n; ++i)
        for(int j = i; j <= n; ++j) 
        {
            scanf("%d", &d[i][j]);
            if(d[i][j] > 0) sum += d[i][j];
        }
    build();
    sum -= dinic();
    printf("%d\n", sum); 
    return 0;
}
View Code

 

bzoj4873