4873: [Shoi2017]寿司餐厅
题意:略
唯一会做的...
一眼最小割
就是最大权闭合子图呀
(s ightarrow d_{positive} ightarrow -d_{negtive} ightarrow t)
然后区间包含关系连inf
然后向t连花费
一开始看成(mx^2 + cx) x是选择种类数,直接吓哭了 平方怎么割啊我只会费用流
然后发现x是编号gg
然后建图注意编号最大是1000,tle了两次....
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 2e4+5, M = 4e6+5, INF = 1e9;
inline int read() {
char c=getchar(); int x=0,f=1;
while(c<'0' || c>'9') {if(c=='-')f=-1; c=getchar();}
while(c>='0' && c<='9') {x=x*10+c-'0'; c=getchar();}
return x*f;
}
int n, m, a[105], d[105][105], s, t, sum, mx;
struct edge{int v, ne, c, f;} e[M];
int cnt=1, h[N];
inline void ins(int u, int v, int c) { //printf("ins (%d, %d) %d
", u, v, c);
e[++cnt] = (edge){v, h[u], c, 0}; h[u] = cnt;
e[++cnt] = (edge){u, h[v], 0, 0}; h[v] = cnt;
}
namespace mf {
int vis[N], d[N], q[N], head, tail;
bool bfs() {
memset(vis, 0, sizeof(vis));
head = tail = 1;
q[tail++] = s; d[s] = 0; vis[s] = 1;
while(head != tail) {
int u = q[head++];
for(int i=h[u];i;i=e[i].ne)
if(!vis[e[i].v] && e[i].c > e[i].f) {
int v = e[i].v;
d[v] = d[u]+1; vis[v] = 1;
q[tail++] = v;
if(v == t) return true;
}
}
return false;
}
int cur[N];
int dfs(int u, int a) {
if(u == t || a == 0) return a;
int flow = 0, f;
for(int &i=cur[u];i;i=e[i].ne)
if(d[e[i].v] == d[u]+1 && (f = dfs(e[i].v, min(a, e[i].c - e[i].f))) > 0) {
flow += f;
e[i].f += f;
e[i^1].f -= f;
a -= f;
if(a == 0) break;
}
if(a) d[u] = -1;
return flow;
}
int dinic() {
int flow = 0;
while(bfs()) {
for(int i=s; i<=t; i++) cur[i] = h[i];
flow += dfs(s, INF);
}
return flow;
}
}
inline int id(int i, int j) {return i==j ? i : i*n+j;}
int vis[N];
void build() {
int n2 = n*n, n3 = n+n2;
s = 0; t = n + n2 + mx + 1;
for(int i=1; i<=n; i++) {
if(d[i][i] > 0) ins(s, i, d[i][i]);
else ins(i, t, -d[i][i]);
ins(i, t, a[i]);
if(m == 0) continue;
ins(i, n3 + a[i], INF);
if(!vis[a[i]]) ins(n3 + a[i], t, a[i] * a[i]), vis[a[i]] = 1;
}
for(int i=n; i>=1; i--)
for(int j=i+1; j<=n; j++) {
int now = id(i, j);
if(d[i][j] > 0) ins(s, now, d[i][j]);
else {ins(now, t, -d[i][j]); continue;}
int rr = 0;
for(int l=i; l<=j; l++) {
int _ = max(i, rr);
for(int r= l==i ? j-1 : j; r>=_; r--) {
ins(now, id(l, r), INF);
if(d[l][r] > 0) { rr = max(rr, r); break; }
}
}
}
}
int main() {
freopen("in", "r", stdin);
n=read(); m=read();
for(int i=1; i<=n; i++) a[i] = read(), mx = max(mx, a[i]);
for(int i=1; i<=n; i++)
for(int j=i; j<=n; j++) {
d[i][j] = read();
if(d[i][j] > 0) sum += d[i][j];
}
build();
int ans = mf::dinic(); //printf("ans %d
", ans);
printf("%d
", sum - ans);
}