zoukankan      html  css  js  c++  java
  • USACO 4.2 The Perfect Stall 完美的牛栏(最大匹配)

    Description

    农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术。不幸的是,由于工程问题,每个牛栏都不一样。第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们喜欢的那些牛栏中产奶。上个星期,农夫约翰刚刚收集到了奶牛们的爱好的信息(每头奶牛喜欢在哪些牛栏产奶)。一个牛栏只能容纳一头奶牛,当然,一头奶牛只能在一个牛栏中产奶。 
    给出奶牛们的爱好的信息,计算最大分配方案。 
    

    Input

    第一行 两个整数,N (0 <= N <= 200) 和 M (0 <= M <= 200) 。N 是农夫约翰的奶牛数量,M 是新牛棚的牛栏数量。 
    第二行到第N+1行 一共 N 行,每行对应一只奶牛。第一个数字 (Si) 是这头奶牛愿意在其中产奶的牛栏的数目 (0 <= Si <= M) 。后面的 Si 个数表示这些牛栏的编号。牛栏的编号限定在区间 (1..M) 中,在同一行,一个牛栏不会被列出两次。 
    

    Output

    只有一行。输出一个整数,表示最多能分配到的牛栏的数量。
    

    题解

    网络流模板。
    

    代码

    const
      maxn=201;
    var
      n,m,ans:longint;
      v:array [0..maxn] of boolean;
      q:array [0..maxn] of longint;
      c:array [0..maxn,0..maxn] of boolean;
    procedure init;
    var
      i,j,nm,y:longint;
    begin
      fillchar(c,sizeof(c),0);
      fillchar(q,sizeof(q),255);
      readln(n,m);
      for i:=1 to n do
        begin
          read(nm);
          for j:=1 to nm do
            begin
              read(y);
              c[i,y]:=true;
            end;
        end;
    end;
    
    function dfs(x:longint):boolean;
    var
      i,j:longint;
    begin
      for i:=1 to m do
        if (c[x,i]) and (not v[i]) then
          begin
            v[i]:=true;
            j:=q[i];
            q[i]:=x;
            if (j=-1) or (dfs(j)) then exit(true);
            q[i]:=j;
          end;
      exit(false);
    end;
    
    procedure main;
    var
      i:longint;
    begin
      ans:=0;
      for i:=1 to n do
        begin
          fillchar(v,sizeof(v),0);
          if dfs(i) then inc(ans);
        end;
      writeln(ans);
    end;
    
    begin
      init;
      main;
    end.
    
    
  • 相关阅读:
    java进程占用CPU高的问题
    上下文切换的案例以及CPU使用率
    百度地图sdk问题 error inflating class com.baidu.mapapi.map.mapview
    Android APK反编译
    Binary XML file line #17<vector> tag requires viewportWidth > 0
    用代码实现签名
    百度地图出现UnsatisfiedLinkError: Native method not found: com.baidu...
    INSTALL_FAILED_INVALID_APK
    Android package属性、package name和Application ID三者的联系及区别
    Android使用百度定位API时获取的地址信息为null
  • 原文地址:https://www.cnblogs.com/zyx-crying/p/9319529.html
Copyright © 2011-2022 走看看