1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| # include<algorithm> # include<iostream> # include<cstring> # include<cstdio> # include<queue>
using namespace std;
inline bool read(int& x){ x = 0; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '\n') return false; ch = getchar(); } while(ch >= '0' && ch <= '9'){ x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); } if(ch == '\n') return false; return true; }
const int N = 155; const int INF = 1e9;
int n, m, S, T, p, tot;
struct Edge{ int nxt, from, to, cap; }edge[N*N<<1]; int head[N], edgeNum = 1; void addEdge(int from, int to, int cap){ edge[++edgeNum] = (Edge){head[from], from, to, cap}; head[from] = edgeNum; }
int lay[N]; bool bfs(){ memset(lay, 0, sizeof lay); queue<int> q; q.push(S); lay[S] = 1; while(!q.empty()){ int cur = q.front(); q.pop(); for(int i = head[cur]; i; i = edge[i].nxt){ if(edge[i].cap <= 0) continue; if(!lay[edge[i].to]){ lay[edge[i].to] = lay[cur] + 1; q.push(edge[i].to); } } } return lay[T]; } int dfs(int x, int minCap){ if(x == T) return minCap; for(int i = head[x]; i; i = edge[i].nxt){ if(edge[i].cap <= 0 || lay[edge[i].to] != lay[x] + 1) continue; int t = dfs(edge[i].to, min(minCap, edge[i].cap)); if(t){ edge[i].cap -= t; edge[i^1].cap += t; return t; } } return 0; } int dinic(){ int maxFlow = 0; while(bfs()) if(int flow = dfs(S, INF)) maxFlow += flow; return maxFlow; }
int main(){ read(m), read(n); S = 101, T = 102; for(int i = 1; i <= m; i++){ read(p); tot += p; addEdge(S, i, p); addEdge(i, S, 0); while(read(p)){ addEdge(i, p+50, INF); addEdge(p+50, i, 0); } addEdge(i, p+50, INF); addEdge(p+50, i, 0); } for(int i = 1; i <= n; i++){ read(p); addEdge(i+50, T, p); addEdge(T, i+50, 0); } int ans = tot - dinic(); for(int i = 1; i <= m; i++) if(lay[i]) printf("%d ", i); putchar(10); for(int i = 1; i <= n; i++) if(lay[i+50]) printf("%d ", i); printf("\n%d\n", ans); return 0; }
|