2016 ACM/ICPC ECNA Regional I.Waif Until Dark(最大流)
这是一道ECNA的16年题,问有n个小朋友,m个玩具,不同孩子有不同喜好的玩具,每个玩具可能属于一个类别,同一类别的玩具最多只能用一定次数,问最大匹配
这个就很裸的二分图,掏出dinic板子,首先最后问的是孩子,所以最后一层肯定是孩子,然后有玩具,所以玩具是和孩子连边的,有种类,那种类和玩具连边,然后如果不属于任何一组的玩具边的容量就是1,与源连上,如果属于的话就和组连上,容量为最大限制,孩子和终点连边,跑一次dinic就行了,easy
#include <bits/stdc++.h> using namespace std; #define limit (90000 + 5)//防止溢出 #define INF 0x3f3f3f3f #define inf 0x3f3f3f3f3f #define lowbit(i) i&(-i)//一步两步 #define EPS 1e-6 #define FASTIO ios::sync_with_stdio(false);cin.tie(0); #define ff(a) printf("%d\n",a ); #define pi(a,b) pair<a,b> #define rep(i, a, b) for(ll i = a; i <= b ; ++i) #define per(i, a, b) for(ll i = b ; i >= a ; --i) #define MOD 998244353 #define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next) #define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\acm_01\\data.txt", "rt", stdin) #define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\acm_01\\dabiao.txt", "wt", stdout) #define debug(x) cout<<x<<endl typedef long long ll; typedef unsigned long long ull; inline ll read(){ ll sign = 1, x = 0;char s = getchar(); while(s > '9' || s < '0' ){if(s == '-')sign = -1;s = getchar();} while(s >= '0' && s <= '9'){x = (x << 3) + (x << 1) + s - '0';s = getchar();} return x * sign; }//快读 void write(ll x){ if(x < 0) putchar('-'),x = -x; if(x / 10) write(x / 10); putchar(x % 10 + '0'); } int n,m,vs,ve,p; int layer[limit],head[limit], cnt; struct node{ int to ,next; ll flow, w; }edge[limit]; ll max_flow; void add_one(int u , int v, ll flow = 0){ edge[cnt].to = v; edge[cnt].next = head[u]; edge[cnt].flow = flow; edge[cnt].w = 0; head[u] = cnt++; } inline void add(int u, int v, ll flow){ add_one(u,v,flow); add_one(v, u,0); } inline void init(bool flag = true){ if(flag){ memset(head, -1, sizeof(head)); cnt = 0; }else{ memset(layer, -1, sizeof(layer)); } } inline bool bfs(){ init(false); queue<int>q; layer[vs] = 0;//从第0层开始 q.push(vs); while (q.size()){ int u = q.front(); q.pop(); traverse(u){ int v = edge[i].to,flow = edge[i].flow; if(layer[v] == -1 && flow > 0){ layer[v] = layer[u] + 1;//迭代加深 q.push(v); } } } return ~layer[ve]; } ll dfs(int u, ll flow){ if(u == ve)return flow; ll rev_flow = 0,min_flow; traverse(u){ int v =edge[i].to; ll t_flow = edge[i].flow; if(layer[v] == layer[u] + 1 && t_flow > 0){ min_flow = dfs(v, min(flow, t_flow)); flow -= min_flow; edge[i].flow -= min_flow; rev_flow += min_flow; edge[i^1].flow += min_flow; if(!flow)break; } } if(!rev_flow)layer[u] = -1; return rev_flow; } void dinic(){ while (bfs()){ max_flow += dfs(vs,inf); } } int vis[limit]; int main() { #ifdef LOCAL FOPEN; #endif init(); n = read(), m = read(), p = read(); vs = 80001, ve = vs + 1; rep(i, 1,n){ add(m + p + i, ve, 1);//孩子 } rep(i ,1,n){ //连孩子和玩具 int op = read(); while (op--){ int num_toy = read(); add(p + num_toy , m + p + i, 1); } } rep(q,1,p){ int l = read(); while (l--){ int x = read(); vis[x] = 1; add(q, p + x, 1); } int r = read(); add(vs,q,r); } rep(i ,1,m)if(!vis[i])add(vs, p + i, 1); dinic(); write(max_flow); return 0; }
天才选手zerol的主页:https://zerol.me/
|
WeepingDemon的个人主页:https://weepingdemon.gitee.io/blog/