图中的保护关系就类似于最大权闭合子图。即你想杀x,你就一定要杀掉保护x的点,那么把x向保护它的点连边。那么题目就转化成了最大权闭合子图的问题。
但是这个图有点特殊啊。。。 考虑有环的情况,显然这个环以及指向这个环的点都不能选。
所以还要把这个图的反图进行一遍拓扑排序,这样忽略掉了这些点了。。。
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi acos(-1.0)
# define eps 1e-
# define MOD
# define INF
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<,l,mid
# define rch p<<|,mid+,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N=;
//Code begin... struct Edge{int p, next, w;}edge[N*N];
int head[N], cnt=, cost[N], dee[N], n, m, s, t, vis[N];
bool mark[N];
queue<int>Q;
vector<PII> E[N]; void add_edge(int u, int v, int w){
edge[cnt].p=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++;
edge[cnt].p=u; edge[cnt].w=; edge[cnt].next=head[v]; head[v]=cnt++;
}
int bfs(){
int i, v;
mem(vis,-);
vis[s]=; Q.push(s);
while (!Q.empty()) {
v=Q.front(); Q.pop();
for (i=head[v]; i; i=edge[i].next) {
if (edge[i].w> && vis[edge[i].p]==-) {
vis[edge[i].p]=vis[v] + ;
Q.push(edge[i].p);
}
}
}
return vis[t]!=-;
}
int dfs(int x, int low){
int i, a, temp=low;
if (x==t) return low;
for (i=head[x]; i; i=edge[i].next) {
if (edge[i].w> && vis[edge[i].p]==vis[x]+){
a=dfs(edge[i].p,min(edge[i].w,temp));
temp-=a; edge[i].w-=a; edge[i^].w += a;
if (temp==) break;
}
}
if (temp==low) vis[x]=-;
return low-temp;
}
void Topsort(){
FO(i,,n*m) if (!dee[i]) Q.push(i), mark[i]=true;
while (!Q.empty()) {
int u=Q.front(); Q.pop();
FO(i,,E[u].size()) {
PII v=E[u][i];
if (!v.second) continue;
--dee[v.first];
if (!dee[v.first]) Q.push(v.first), mark[v.first]=true;
}
}
}
int main ()
{
int T, x, y, res=, tmp, sum=;
scanf("%d%d",&n,&m); s=n*m; t=n*m+;
FO(i,,n) FO(j,,m) {
scanf("%d%d",&cost[i*m+j],&T);
while (T--) {
scanf("%d%d",&x,&y); E[x*m+y].pb(mp(i*m+j,));
E[i*m+j].pb(mp(x*m+y,)); ++dee[x*m+y];
}
}
FO(i,,n) FO(j,,m-) FO(k,j+,m) E[i*m+j].pb(mp(i*m+k,)), E[i*m+k].pb(mp(i*m+j,)), ++dee[i*m+j];
Topsort();
FO(i,,n*m) {
if (!mark[i]) continue;
if (cost[i]>=) res+=cost[i], add_edge(s,i,cost[i]);
else add_edge(i,t,-cost[i]);
FO(j,,E[i].size()) {
PII v=E[i][j];
if (v.second||!mark[v.first]) continue;
add_edge(i,v.first,INF);
}
}
while (bfs()) while (tmp=dfs(s,INF)) sum+=tmp;
printf("%d\n",res-sum);
return ;
}