CF of C title so difficult, the autistic.
Face questions
https://www.luogu.org/problem/CF802C
answer
In addition to the second cut, cost flow problem is generally divided into two kinds:
Thinking in terms of points: column flow conservation equations, the equation seen as a point, as the variable side, there are the typical volunteer recruitment , Delight for a Cat.
Thinking in terms of flows: a representative of a process stream, typically have napkins program issues and the longest interval k can be re-set issues .
model:
Classification model: the smallest cut can be seen as an enhanced version, such as the sequence 48 minute practice.
Adjustment model: Fang Bobo transport coconut , the team returns .
From the perspective of the stream of thinking a good question, but also my first line of minimum cost flow problem.
First, the flow amount of control points is $ K $, representative of the bookcase capacity $ k $. A change of the flow capacity of a representative of the change in the position of the bookshelf.
Each point is split and $ X $ $ x '$, $ 1 $ capacity even with a value of $ $ -INF side, once passed from above, will flow value sharply. Such flows can be distinguished in order to pursue "maximum" or "minimum value" (value stream to see whether the $ <0 $)
Then for $ i, j (i <j) $ connected edge $ (i ', j, 1, c [a [j]]) $, on behalf if the shelf if originally satisfies the first $ I $ a requirement to the first $ j $ to meet the requirement to pay the cost of buying $ j $ (if $ a [i] = a [j] $ then do not pay)
This will be all right.
#include<map> #include<stack> #include<queue> #include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define N 500 #define S 0 #define T (2*n+1) #define LL long long #define ri register int #define inf 1000000007 using namespace std; int n,k; int a[N],c[N]; struct graph { vector<int> to,w,c,ed[N]; LL dis[N]; int cur[N]; bool vis[N]; void add_edge(int a,int b,int aw,int ac) { to.push_back(b); w.push_back(aw); c.push_back(ac); ed[a].push_back(to.size()-1); to.push_back(a); w.push_back(0); c.push_back(-ac); ed[b].push_back(to.size()-1); } bool spfa() { memset(dis,0x3f,sizeof(dis)); memset(vis,0,sizeof(vis)); queue<int> q; dis[S]=0;q.push(S);vis[S]=1; while (!q.empty()) { int x=q.front(); q.pop(); for (ri i=0;i<ed[x].size();i++) { int e=ed[x][i]; if (dis[to[e]]>dis[x]+c[e] && w[e]) { dis[to[e]]=dis[x]+c[e]; if (!vis[to[e]]) vis[to[e]]=1,q.push(to[e]); } } vis[x]=0; } return dis[T]<inf; } int dfs(int x,int lim) { if (x==T || !lim) return lim; LL sum=0; vis[x]=1; for (ri &i=cur[x];i<ed[x].size();i++) { int e=ed[x][i]; if (dis[x]+c[e]==dis[to[e]] && w[e] && !vis[to[e]]) { int f=dfs(to[e],min(lim,w[e])); w[e]-=f; w[1^e]+=f; lim-=f; sum+=f; if (!lim) return sum; } } return sum; } LL zkw() { LL ret=0; while (spfa()) { memset(vis,0,sizeof(vis)); memset(cur,0,sizeof(cur)); int f=dfs(S,n); k-=f; if (k<0) { if ((k+f)*dis[T]<0) ret+=(k+f)*dis[T]; return ret; } if (f*dis[T]<0) ret+=f*dis[T]; } return ret; } } G; int main() { scanf("%d %d",&n,&k); for (ri i=1;i<=n;i++) scanf("%d",&a[i]); for (ri i=1;i<=n;i++) scanf("%d",&c[i]); for (ri i=1;i<=n;i++) G.add_edge(S,i,1,c[a[i]]); for (ri i=1;i<=n;i++) G.add_edge(i,n+i,1,-inf); for (ri i=1;i<=n;i++) G.add_edge(n+i,T,1,0); for (ri i=1;i<n;i++) { for (ri j=i+1;j<=n;j++) if (a[i]!=a[j]) G.add_edge(n+i,j,1,c[a[j]]); else G.add_edge(n+i,j,1,0); } cout<<G.zkw()+n*1LL*inf<<endl; return 0; }