很明显是最大权封闭子图(noi挺喜欢考?),但直接跑最小割的话处理不了不能打到的plant,所以要先拓扑排序简化下图
(重建图挺烦。。)
1 #include<bits/stdc++.h> 2 #define inc(i,l,r) for(int i=l;i<=r;i++) 3 #define dec(i,l,r) for(int i=l;i>=r;i--) 4 #define link(x) for(edge *j=h[x];j;j=j->next) 5 #define mem(a) memset(a,0,sizeof(a)) 6 #define inf 1e9 7 #define ll long long 8 #define succ(x) (1<<x) 9 #define NM 1000 10 #define nm 1000000 11 using namespace std; 12 int read(){ 13 int x=0,f=1;char ch=getchar(); 14 while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} 15 while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); 16 return x*f; 17 } 18 struct edge{ 19 int t,v; 20 edge *next,*rev; 21 }_e[nm],e[nm],*h[NM],*_h[NM],*o=_e,*tmp[NM],*p[NM]; 22 void __add(int x,int y){ 23 o++;o->t=y;o->next=_h[x];_h[x]=o; 24 } 25 void _add(int x,int y,int v){ 26 o++;o->t=y;o->v=v;o->next=h[x];h[x]=o; 27 } 28 void add(int x,int y,int v){ 29 _add(x,y,v);_add(y,x,0); 30 h[x]->rev=h[y];h[y]->rev=h[x]; 31 } 32 int n,m,b[25][35],_x,_y,_t,_s,v[NM],d[NM],tot,cnt[NM],a[NM]; 33 queue<int >q; 34 void bfs(){ 35 inc(i,1,tot)if(!v[i])q.push(i); 36 while(!q.empty()){ 37 int t=q.front();q.pop(); 38 for(edge *j=_h[t];j;j=j->next)if(v[j->t]){ 39 v[j->t]--; 40 if(!v[j->t])q.push(j->t); 41 } 42 } 43 } 44 int maxflow(){ 45 int flow=0;edge *j; 46 inc(i,0,n)tmp[i]=h[i]; 47 cnt[0]=tot; 48 for(int x=0,s=inf;d[x]<tot;){ 49 for(j=tmp[x];j;j=j->next) 50 if(j->v&&d[x]==d[j->t]+1)break; 51 if(j){ 52 s=min(s,j->v);tmp[x]=p[j->t]=j; 53 if((x=j->t)==n){ 54 for(;x;x=p[x]->rev->t) 55 p[x]->v-=s,p[x]->rev->v+=s; 56 flow+=s;s=inf; 57 } 58 }else{ 59 if(!--cnt[d[x]])break; 60 d[x]=tot; 61 link(x)if(j->v&&d[x]>d[j->t]+1) 62 d[x]=d[j->t]+1,tmp[x]=j; 63 if(x)x=p[x]->rev->t; 64 } 65 } 66 return flow; 67 } 68 int main(){ 69 // freopen("data.in","r",stdin); 70 n=read();m=read(); 71 inc(i,1,n) 72 inc(j,1,m)b[i][j]=++tot; 73 inc(i,1,n) 74 inc(j,1,m){ 75 _t=read(); 76 a[b[i][j]]=_t; 77 if(_t>0)add(b[i][j],tot+1,_t); 78 if(_t<0)add(0,b[i][j],-_t); 79 _t=read(); 80 inc(k,1,_t){ 81 _x=read()+1;_y=read()+1; 82 __add(b[i][j],b[_x][_y]);v[b[_x][_y]]++; 83 } 84 if(j>1)__add(b[i][j],b[i][j-1]),v[b[i][j-1]]++; 85 } 86 bfs();o=e; 87 inc(i,1,tot)if(!v[i]){ 88 if(a[i]>0)_s+=a[i]; 89 for(edge *j=_h[i];j;j=j->next)if(!v[j->t])add(i,j->t,inf); 90 } 91 n=tot+1;tot=n+1; 92 printf("%d\n",_s-maxflow()); 93 return 0; 94 }