首页 > 代码库 > bzoj4873 [Shoi2017]寿司餐厅

bzoj4873 [Shoi2017]寿司餐厅

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

【题解】

没看出来是最大权闭合子图模型……要多学习学习qwq

首先区间$[i,j]$依赖于区间$[i+1,j]$和$[i,j-1]$。每个区间$[i,j](i < j)$的权值就是$d_{i,j}$。

特别地,区间$[i, i]$的权值为$d_{i,j} - a_i$(由于花费原因)。

区间$[i, i]$还依赖于$a_i$这个点,$a_i$这个点的权值为$-m * a_i * a_i$(花费)。

然后直接跑最大权闭合子图模型即可。

时间复杂度O(能过)。

这个区间建模有点意思,看来知识水平不够高啊qwq

技术分享
# include <queue>
# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 8e5 + 10, N = 1e2 + 10;
const int mod = 1e9+7;
const ll inf = 1e16;


int n, K, idx, S, T;
ll sum = 0;
vector<int> ps;
int a[N], d[N][N], id[N][N], bid[N * 10]; 

namespace MF {
    int head[M], nxt[M], to[M], tot = 1;
    ll flow[M]; 
    inline void add(int u, int v, ll fl) {
        ++tot; nxt[tot] = head[u]; head[u] = tot; to[tot] = v; flow[tot] = fl;
    }
    inline void adde(int u, int v, ll fl) {
        add(u, v, fl), add(v, u, 0ll);
    }
    queue<int> q; int c[M], cur[M];
    inline bool bfs() {
         while(!q.empty()) q.pop();
        for (int i=1; i<=idx; ++i) c[i] = -1;
        c[S] = 0; q.push(S);
        while(!q.empty()) {
            int top = q.front(); q.pop();
            for (int i=head[top]; i; i=nxt[i]) {
                if(c[to[i]] != -1 || !flow[i]) continue;
                c[to[i]] = c[top] + 1;
                q.push(to[i]);
                if(to[i] == T) return 1;
            }
        }
        return 0;
    }
    inline ll dfs(int x, ll low) {
        if(x == T) return low;
        ll r = low, fl;
        for (int i=cur[x]; i; i=nxt[i]) {
             if(c[to[i]] != c[x] + 1 || !flow[i]) continue;
            fl = dfs(to[i], min(r, flow[i]));
            flow[i] -= fl; r -= fl; flow[i^1] += fl;
            if(flow[i] > 0) cur[x] = i;
            if(!r) return low;
        }
        if(low == r) c[x] = -1;
        return low-r;
    }
    inline ll main() {
        ll ret = 0;
        while(bfs()) {
            for (int i=1; i<=idx; ++i) cur[i] = head[i];
            ret += dfs(S, inf);
        }
        return ret;
    }    
}

inline void add(int u, ll d) {
    if(d >= 0) MF :: adde(S, u, d), sum += d; 
    else MF :: adde(u, T, -d);
}

int main() {
    cin >> n >> K;
    for (int i=1; i<=n; ++i) {
        scanf("%d", &a[i]);
        ps.push_back(a[i]);
    }
    S = ++idx, T = ++idx;
    for (int i=1; i<=n; ++i)
        for (int j=i; j<=n; ++j) {
            scanf("%d", &d[i][j]);
            id[i][j] = ++idx;
            if(i == j) d[i][j] -= a[i]; 
        }
    
    for (int i=1; i<=n; ++i) 
        for (int j=i; j<=n; ++j) 
            add(id[i][j], d[i][j]);
             
    sort(ps.begin(), ps.end()); 
    ps.erase(unique(ps.begin(), ps.end()), ps.end());
     
    for (int i=0; i<ps.size(); ++i) {
        bid[ps[i]] = ++idx;
        add(bid[ps[i]], -1ll * K * ps[i] * ps[i]); 
    }
    
    for (int i=1; i<=n; ++i) { 
        for (int j=i+1; j<=n; ++j) {
             MF :: adde(id[i][j], id[i+1][j], inf);
            MF :: adde(id[i][j], id[i][j-1], inf);
        }
        MF :: adde(id[i][i], bid[a[i]], inf);
    }
    
    cout << max(sum - MF :: main(), 0ll); 
         
    return 0;
}
View Code

 

bzoj4873 [Shoi2017]寿司餐厅