题意分析
如题
注意边数是约束条件,不是顶点的个数。
代码总览
#include<bits/stdc++.h>
using namespace std;
const int nmax = 10000;
const int INF = 0x3f3f3f3f;
typedef long long ll;
typedef struct{
int to,nxt;
ll w;
}Edge;
Edge e[nmax<<1];
int cur[nmax], dep[nmax], head[nmax] ,S, T, tot,n,m;
ll w,ans = 0;
void add(int u, int v, ll w){
e[tot].to = v;
e[tot].nxt = head[u];
e[tot].w = w;
head[u] = tot ++;
}
bool bfs(){
memset(dep,-1,sizeof dep); dep[S] = 0;
queue<int> q; q.push(S);
while(!q.empty()){
int now = q.front(); q.pop();
for(int i = head[now]; i!= -1; i = e[i].nxt){
int v = e[i].to;
if(dep[v] == -1 && e[i].w){
dep[v] = dep[now] + 1;
q.push(v);
}
}
}
return dep[T] !=-1;
}
ll dfs(int now, ll flow){
if(now == T || !flow) return flow;
ll f = 0;
for(int & i = cur[now]; i!=-1; i = e[i].nxt){
int v = e[i].to;
if(dep[v] == dep[now] + 1 && e[i].w){
ll tt = dfs(v,min(flow,e[i].w));
if(tt){
e[i].w -= tt; e[i^1].w += tt;
flow -= tt;
f += tt;
if(flow == 0) break;
}
}
}
return f;
}
void dinic(){
while(bfs()){
for(int i = 0;i<=T;++i) cur[i] = head[i];
while(ll tt = dfs(S,INF)) ans -= tt;
}
}
int main(){
scanf("%d %d",&n,&m);
memset(head,-1,sizeof head); tot = 0;
S = 0; T = n+m+1;
for(int i = 1;i<=m;++i){
scanf("%lld",&w);
add(i+n,T,w); add(T,i+n,0);
}
int nn,num;
for(int i = 1;i<=n;++i){
scanf("%lld %d",&w,&nn);
ans += w;
add(S,i,w);
add(i,S,0);
for(int j = 1;j<=nn;++j){
scanf("%d",&num);
add(i,num+n,INF);
add(num+n,i,0);
}
}
dinic();
printf("%lld\n",ans);
return 0;
}