zoukankan      html  css  js  c++  java
  • hdu3605 Escape 二分图多重匹配/最大流

    2012 If this is the end of the world how to do? I do not know how. But now scientists have found that some stars, who can live, but some people do not fit to live some of the planet. Now scientists want your help, is to determine what all of people can live in these planets.

    题意:有N个人,每个人能适应若干个星球(共10个星球),每个星球能够容纳若干人,问是否能让所有人都有星球住。

    其实应该是一个二分图多重匹配的题目。

    不过一开始当成最大流的题目了,做的时候TLE了,然后将对10个星球的适应性的情况用状压变成了2^10种情况,即2^10种人,并且加上读入优化在vj上也卡过去了,不过在杭电上始终还是TLE了。

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<math.h>
     4 #include<algorithm>
     5 using namespace std;
     6 
     7 const int maxn=100010;
     8 const int maxm=11;
     9 int match[maxn][maxm],g[maxn][maxm],cnt[maxm],lim[maxn],n,m;
    10 bool vis[maxm];
    11 
    12 bool dfs(int s){
    13     for(int i=0;i<m;++i){
    14         if(g[s][i]==0||vis[i]==1)continue;
    15         vis[i]=1;
    16         if(cnt[i]<lim[i]){
    17             match[i][cnt[i]++]=s;
    18             return 1;
    19         }
    20         else{
    21             for(int j=0;j<lim[i];++j){
    22                 if(dfs(match[i][j])==1){
    23                     match[i][j]=s;
    24                     return 1;
    25                 }
    26             }
    27         }
    28     }
    29     return 0;
    30 }
    31 bool hungary(int n){
    32     for(int i=0;i<n;++i){
    33         memset(vis,0,sizeof(vis));
    34         if(dfs(i)==0)return 0;
    35     }
    36     return 1;
    37 }
    38 int main(){
    39     while(scanf("%d%d",&n,&m)!=EOF){
    40         memset(cnt,0,sizeof(cnt));
    41         for(int i=0;i<n;++i)
    42             for(int j=0;j<m;++j)scanf("%d",&g[i][j]);
    43         for(int i=0;i<m;++i)scanf("%d",&lim[i]);
    44         if(hungary(n))printf("YES
    ");
    45         else printf("NO
    ");
    46     }
    47     return 0;
    48 }
    多重匹配
      1 #pragma comment(linker,"/STACK:16777216")
      2 #include<stdio.h>
      3 #include<string.h>
      4 #include<vector>
      5 #include<queue>
      6 #include<algorithm>
      7 using namespace std;
      8 const int maxm=1050;
      9 const int INF=0x3f3f3f3f;
     10 
     11 struct edge{
     12     int from,to,c,f;
     13     edge(int a,int b,int m,int n):from(a),to(b),c(m),f(n){}
     14 };
     15 
     16 struct dinic{
     17     int s,t,m;
     18     vector<edge>e;
     19     vector<int>g[maxm];
     20     bool vis[maxm];
     21     int cur[maxm],d[maxm];
     22 
     23     void init(int n){
     24         for(int i=0;i<=n;i++)g[i].clear();
     25         e.clear();
     26     }
     27 
     28     void add(int a,int b,int c){
     29         e.push_back(edge(a,b,c,0));
     30         e.push_back(edge(b,a,0,0));
     31         m=e.size();
     32         g[a].push_back(m-2);
     33         g[b].push_back(m-1);
     34     }
     35 
     36     bool bfs(){
     37         memset(vis,0,sizeof(vis));
     38         queue<int>q;
     39         q.push(s);
     40         vis[s]=1;
     41         d[s]=0;
     42         while(!q.empty()){
     43             int u=q.front();
     44             q.pop();
     45             for(int i=0;i<g[u].size();i++){
     46                 edge tmp=e[g[u][i]];
     47                 if(!vis[tmp.to]&&tmp.c>tmp.f){
     48                     d[tmp.to]=d[u]+1;
     49                     vis[tmp.to]=1;
     50                     q.push(tmp.to);
     51                 }
     52             }
     53         }
     54         return vis[t];
     55     }
     56 
     57     int dfs(int x,int a){
     58         if(x==t||a==0)return a;
     59         int flow=0,f;
     60         for(int& i=cur[x];i<g[x].size();i++){
     61             edge& tmp=e[g[x][i]];
     62             if(d[tmp.to]==d[x]+1&&(f=dfs(tmp.to,min(a,tmp.c-tmp.f)))>0){
     63                 tmp.f+=f;
     64                 e[g[x][i]^1].f-=f;
     65                 flow+=f;
     66                 a-=f;
     67                 if(a==0)break;
     68             }
     69         }
     70         if(!flow)d[x]=-1;
     71         return flow;
     72     }
     73 
     74     int mf(int s,int t){
     75         this->s=s;
     76         this->t=t;
     77         int flow=0;
     78         while(bfs()){
     79             memset(cur,0,sizeof(cur));
     80             flow+=dfs(s,INF);
     81         }
     82         return flow;
     83     }
     84 };
     85 
     86 int num[1050];
     87 
     88 int main(){
     89     int n,m;
     90     while(scanf("%d%d",&n,&m)!=EOF){
     91         memset(num,0,sizeof(num));
     92         dinic d;
     93         d.init(m+(1<<m)+5);
     94         int i,j;
     95         for(i=1;i<=n;i++){
     96             int tmp=0;
     97             for(j=1;j<=m;j++){
     98                 int a=0;
     99                 char c=getchar();
    100                 while(c>'9'||c<'0')c=getchar();
    101                 a=c-'0';
    102                 tmp=tmp*2+a;
    103             }
    104             num[tmp]++;
    105         }
    106         for(i=1;i<=(1<<m)-1;i++){
    107             if(num[i]){
    108                 d.add(0,i,num[i]);
    109                 int tmp=i;
    110                 for(j=m;j>=1;j--){
    111                     if(tmp&1){
    112                         d.add(i,(1<<m)+j,INF);
    113                     }
    114                     tmp>>=1;
    115                 }
    116             }
    117         }
    118         for(j=1;j<=m;j++){
    119             int a=0;
    120             char c=getchar();
    121             while(c>'9'||c<'0')c=getchar();
    122             while(c>='0'&&c<='9'){
    123                 a=a*10+c-'0';
    124                 c=getchar();
    125             }
    126             d.add((1<<m)+j,m+(1<<m)+1,a);
    127         }
    128         if(n==d.mf(0,m+(1<<m)+1))printf("YES
    ");
    129         else printf("NO
    ");
    130     }
    131     return 0;
    132 }
    最大流
  • 相关阅读:
    P1149 火柴棒等式
    SpringMVC之reset风格和form表单格式的curd
    SpringMVC之转发重定向
    文件下载
    文件上传
    数据库分页
    使用代理创建连接池 proxyPool
    Proxy 示例
    Proxy基础---------获取collection接口的构造跟方法
    javaBean中 字符串 转 date 类型转换
  • 原文地址:https://www.cnblogs.com/cenariusxz/p/6592556.html
Copyright © 2011-2022 走看看