注意审清题意…
看错题导致浪费了一上午的时间
发现其实就是个最大闭合子权图的水题
[l,r]
[
l
,
r
]
肯定要向
[l+1,r]
[
l
+
1
,
r
]
[l,r−1]
[
l
,
r
−
1
]
连边
s
s
向每一个值大于的区间连边,所有值小于
0
0
的区间向连边
把每一种寿司单独做一个点,选每一个点的时候再连过来即可
c++代码如下:
#include<bits/stdc++.h>
#define id(i,j) ((i-1)*n+j)
#define rep(i,x,y) for(register int i = x ; i <= y; ++ i)
#define repd(i,x,y) for(register int i = x ; i >= y; -- i)
using namespace std;
typedef long long ll;
template<typename T>inline void read(T&x)
{
char c; int sign = 1; x = 0;
do { c = getchar(); if(c == '-') sign = -1; }while(!isdigit(c));
do { x = x * 10 + c - '0'; c = getchar(); }while(isdigit(c));
x *= sign;
}
const int N = 101,M = 1e6+50,inf = 1e9+7;
int n,m,s,t,mx,ans,a[N],d[N][N];
int head[M],nxt[M],to[M],f[M],tot;
inline void add(int x,int y,int flow)
{
to[tot] = y;
nxt[tot] = head[x];
f[tot] = flow;
head[x] = tot++;
}
int cur[M],q[M],st,en;
int D[M];
inline bool bfs()
{
memset(D,0,sizeof D);
D[s] = 1; q[st = en = 0] = s;
while(st <= en)
{
int x = q[st++]; cur[x] = head[x];
for(register int i = head[x];~i;i=nxt[i])
if(f[i] && !D[to[i]]){
D[to[i]] = D[x] + 1;
q[++en] = to[i];
}
}
return D[t];
}
int dfs(int x,int w)
{
if(!w || x == t) return w;
int flow = 0,F;
for(register int&i=cur[x];~i;i=nxt[i])
if(D[to[i]] == D[x] + 1 && (F=dfs(to[i],min(w,f[i]))))
{
f[i] -= F;f[i^1] += F;
flow += F; w -= F;
if(!w) break;
}
return flow;
}
int main()
{
memset(head,-1,sizeof head);
read(n); read(m);
rep(i,1,n) read(a[i]),mx = max(mx,a[i]);
rep(i,1,n) rep(j,i,n) read(d[i][j]);
t = n * (n + 2) + mx;
rep(i,1,mx) add(n*n+i,t,m * i * i),add(t,n*n+i,0);
rep(i,1,n)
{
add(id(i,i),n*n+a[i],inf),add(n*n+a[i],id(i,i),0);
add(id(i,i),t,a[i]),add(t,id(i,i),0);
}
rep(i,1,n)
rep(j,i,n)
{
if(d[i][j] > 0) add(s,id(i,j),d[i][j]),add(id(i,j),s,0),ans += d[i][j];
if(d[i][j] < 0) add(id(i,j),t,abs(d[i][j])),add(t,id(i,j),0);
if(i == j) continue;
add(id(i,j),id(i + 1,j),inf);
add(id(i + 1,j),id(i,j),0);
add(id(i,j),id(i,j - 1),inf);
add(id(i,j - 1),id(i,j),0);
}
while(bfs())
ans -= dfs(s,inf);
cout << ans << endl;
return 0;
}