码迷,mamicode.com
首页 > 其他好文 > 详细

网络流三·二分图多重匹配 HihoCoder - 1393

时间:2017-10-06 11:33:49      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:pop   blank   set   ide   open   分享   get   ble   one   

网络流三·二分图多重匹配

 HihoCoder - 1393 

 

技术分享
  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 const int maxv = 210;
  4 const int maxe = 10210;
  5 const int inf = 0x3f3f3f3f;
  6 struct Edge{
  7     int u, v, nex;
  8     int cap, flow;
  9     Edge(int u=0, int v=0, int nex=0, int cap=0, int flow=0):
 10         u(u), v(v), nex(nex), cap(cap), flow(flow){}
 11 }e[maxe<<1];
 12 int head[maxv];
 13 int cnt;
 14 void init(){
 15     memset(head, -1, sizeof(head));
 16     cnt = 0;
 17 }
 18 
 19 void add(int u, int v, int cap){
 20     e[cnt] = Edge(u, v, head[u], cap, 0);
 21     head[u] = cnt++;
 22     e[cnt] = Edge(v, u, head[v], 0, 0);
 23     head[v] = cnt++;
 24 }
 25 
 26 int d[maxv], num[maxv], cur[maxv], p[maxv];
 27 int vis[maxv];
 28 int S, T;
 29 int N;
 30 
 31 void bfs(){
 32     memset(d, -1, sizeof(d));
 33     memset(vis, 0, sizeof(vis));
 34     queue<int> q;
 35     q.push(T);
 36     vis[T] = 1;
 37     while(!q.empty()){
 38         int u = q.front();
 39         q.pop();
 40         for(int i = head[u]; ~i; i = e[i].nex){
 41             int id = i&(-2);  //正边
 42             int v = e[id].u;
 43             if(!vis[v] && e[id].cap > e[id].flow){
 44                 vis[v] = 1;
 45                 d[v] = d[u] + 1;
 46                 q.push(v);
 47             }
 48         }
 49     }
 50 }
 51 
 52 int augment(){
 53     int u = T;
 54     int a = inf;
 55     while(u!=S){
 56         int id = p[u];
 57         a = min(a, e[id].cap - e[id].flow);
 58         u = e[id].u;
 59     }
 60     u = T;
 61     while(u!=S){
 62         int id = p[u];
 63         e[id].flow += a;
 64         e[id^1].flow -= a;
 65         u = e[id].u;
 66     }
 67     return a;
 68 }
 69 
 70 int ISAP(){
 71     bfs();
 72     int flow = 0;
 73     memset(num, 0, sizeof(num));
 74     for(int i = 0; i < N; i++){
 75         cur [i] = head[i];
 76         if(~d[i]) num[d[i]]++;
 77     }
 78     int u = S;
 79     while(d[S] < N){
 80         if(u == T){
 81             flow += augment();
 82             u = S;
 83         }
 84         int ok = 0;
 85         for(int i = cur[u]; ~i; i = e[i].nex){
 86             int v = e[i].v;
 87             if(d[u] == d[v]+1 && e[i].cap > e[i].flow){
 88                 p[v] = i;
 89                 ok = 1;
 90                 cur[u] = i;
 91                 u = v;
 92                 break;
 93             }
 94         }
 95         if(!ok){
 96             int m = N-1;
 97             for(int i = head[u]; ~i; i = e[i].nex){
 98                 if(e[i].cap > e[i].flow && ~d[e[i].v]) m = min(m, d[e[i].v]);
 99             }
100             if(--num[d[u]] == 0) break;
101             num[d[u]=m+1]++;
102             cur[u] = head[u];
103             if(u != S) u = e[p[u]].u;
104         }
105     }
106     return flow;
107 }
108 int main(){
109     int t;
110     scanf("%d", &t);
111     int n, m;
112     while(t--) {
113         init();
114         scanf("%d %d", &n, &m);
115         int res = 0;
116         int var;
117         S = 0;
118         T = n+m+1;
119         N = T+1;
120         for(int i = 1; i <= m; i++){
121             scanf("%d", &var);
122             add(n+i, T, var);
123             res += var;
124         }
125         int a, b, x;
126         for(int i = 1; i <= n; i++){
127             scanf("%d %d", &a, &b);
128             add(S, i, a);
129             for(int j = 0; j < b; j++){
130                 scanf("%d", &x);
131                 add(i, x+n, 1);
132             }
133         }
134         int ans = ISAP();
135         if(ans == res) puts("Yes");
136         else puts("No");
137     }
138     return 0;
139 }
View Code

 

网络流三·二分图多重匹配 HihoCoder - 1393

标签:pop   blank   set   ide   open   分享   get   ble   one   

原文地址:http://www.cnblogs.com/yijiull/p/7631125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!