zoukankan      html  css  js  c++  java
  • The Perfect Stall

    poj1274:http://poj.org/problem?id=1274

    题意:有n个奶牛和m个谷仓,现在每个奶牛有自己喜欢去的谷仓,并且它们只会去自己喜欢的谷仓吃东西,问最多有多少奶牛能够吃到东西

    题解:裸的二分图匹配,直接上匈牙利。

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<algorithm>
     5 using namespace std;
     6 const int MAXN=1002;
     7 int n,m,k,w,u,v;
     8 int cy[MAXN];
     9 bool visit[MAXN];
    10 bool g[MAXN][MAXN];
    11 int path(int u){
    12      for(int i=1;i<=m;i++){
    13         if(!visit[i]&&g[u][i]){
    14             visit[i]=1;
    15         if(cy[i]==-1||path(cy[i])){
    16             cy[i]=u;
    17             return 1;
    18         }
    19        }
    20      }
    21    return 0;
    22 }
    23 int maxmatch(){
    24     memset(cy,-1,sizeof(cy));
    25     int res=0;
    26     for(int i=1;i<=n;i++){
    27     memset(visit,0,sizeof(visit));
    28         res+=path(i);
    29     }
    30     return res;
    31 }
    32 int main(){
    33     while(~scanf("%d%d",&n,&m)&&n){
    34             memset(g,0,sizeof(g));
    35           for(int i=1;i<=n;i++){
    36               scanf("%d",&k);
    37           while(k--){
    38             scanf("%d",&w);
    39              g[i][w]=1;
    40           }
    41 
    42           }
    43        printf("%d
    ",maxmatch());
    44     }
    45 }
    View Code
  • 相关阅读:
    简单工厂模式
    原型模式
    特性Attribute
    MVC_Route层层深入
    异步Async
    sql-connectionStrings
    观察者模式(利用委托)
    SqlServer_存储过程
    c语言----程序记录
    c语言基础笔记
  • 原文地址:https://www.cnblogs.com/chujian123/p/3940119.html
Copyright © 2011-2022 走看看