• P2763-试题库问题


      1 #include<bits/stdc++.h>
      2 #define _for(i,a,b) for(register int i = (a);i < b;i ++)
      3 #define _rep(i,a,b) for(register int i = (a);i > b;i --)
      4 #define INF 0x3f3f3f3f
      5 #define MOD 100000000
      6 #define maxn 1000003
      7 #define pb push_back
      8 #define debug() printf("Miku Check OK!
    ")
      9 typedef long long ll;
     10 
     11 using namespace std;
     12 typedef pair<int,int> P;
     13 inline ll read()
     14 {
     15     ll ans = 0;
     16     char ch = getchar(), last = ' ';
     17     while(!isdigit(ch)) last = ch, ch = getchar();
     18     while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
     19     if(last == '-') ans = -ans;
     20     return ans;
     21 }
     22 inline void write(ll x)
     23 {
     24     if(x < 0) x = -x, putchar('-');
     25     if(x >= 10) write(x / 10);
     26     putchar(x % 10 + '0');
     27 }
     28 int ver[maxn],Next[maxn],head[maxn],val[maxn];
     29 int d[maxn];
     30 int n,m,k,s,t,tot,maxflow;
     31 const int N = 2000;
     32 void add(int x,int y,int w)
     33 {
     34     ver[++tot] = y,Next[tot] = head[x],head[x] = tot,val[tot] = w;
     35 }
     36 bool bfs()
     37 {
     38     memset(d,0,sizeof(d));
     39     queue<int> q;
     40     q.push(s);d[s] = 1;
     41     while(!q.empty())
     42     {
     43         int x = q.front();q.pop();
     44         for(int i = head[x]; i; i = Next[i])
     45             if(val[i] && !d[ver[i]])
     46             {
     47                 q.push(ver[i]);
     48                 d[ver[i]] = d[x]+1;
     49                 if(ver[i]==t)
     50                     return true;
     51             }
     52     }
     53     return false;
     54 }
     55 int dinic(int x,int flow)
     56 {
     57     if(x==t) return flow;
     58     // k为子节点增量 
     59     int rest = flow, k;
     60     for(int i = head[x]; i && rest; i = Next[i])
     61     {
     62         if(val[i] && d[ver[i]] == d[x]+1)
     63         {
     64             k = dinic(ver[i],min(rest,val[i]));
     65             if(!k) d[ver[i]] = 0;
     66             val[i] -= k;
     67             val[i^1] += k;
     68             rest -= k;
     69         }
     70     }
     71     return flow - rest;
     72 }
     73 int kNeed[30];
     74 int main()
     75 {
     76     k = read();n = read();
     77     tot = 1;maxflow = 0;m = 0;s = 0;t = 2*N+1;
     78     _for(i,1,k+1)
     79     {
     80         kNeed[i] = read();m += kNeed[i];
     81         add(i+N,2*N+1,kNeed[i]);add(2*N+1,i+N,0);
     82     }
     83     _for(i,1,n+1)
     84     {
     85         int d = read();
     86         _for(j,0,d)
     87         {
     88             int x = read();
     89             add(i,x+N,1);add(x+N,i,0);
     90         }
     91         add(0,i,1);add(i,0,0);
     92     } 
     93     
     94     int flow = 0;
     95     while(bfs()) 
     96         while(flow = dinic(s,INF))
     97             maxflow += flow;
     98     
     99     if(maxflow != m)
    100     {
    101         printf("No Solution!
    ");
    102         return 0;
    103     }
    104     
    105     _for(i,1,k+1)
    106     {
    107         printf("%d: ",i);
    108         for(int j = head[i+N]; j ; j = Next[j])
    109         {
    110             int y = ver[j];
    111             if(y < N && val[j])
    112                 printf("%d ",y);
    113         }
    114         printf("
    ");
    115     } 
    116     return 0;
    117 }
  • 相关阅读:
    复习列表
    20201009 day30 复习2:滑动窗口
    20201009 day30 复习1:扫描线
    20201007day29 模拟(九)
    20201006day28 模拟(八)
    20201005day27 模拟(七)
    20201004 day26 模拟(六)
    20201003day25 模拟(五)
    路由重分布(一)
    RIP路由协议(一)
  • 原文地址:https://www.cnblogs.com/Asurudo/p/11640042.html
Copyright © 2020-2023  润新知