• AcWing P373 車的放置


    Analysis

    这道题是二分图匹配,设可以放車的的地方的坐标为(i,j),则连一条i到j的有向边(注意是有向边),然后再跑匈牙利算法就好了。时间复杂度是O(nm(n+m)),在1n,m200的情况下可以接受。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #define maxn 205
     6 using namespace std;
     7 inline int read() 
     8 {
     9     int x=0;
    10     bool f=1;
    11     char c=getchar();
    12     for(; !isdigit(c); c=getchar()) if(c=='-') f=0;
    13     for(; isdigit(c); c=getchar()) x=(x<<3)+(x<<1)+c-'0';
    14     if(f) return x;
    15     return 0-x;
    16 }
    17 inline void write(int x)
    18 {
    19     if(x<0){putchar('-');x=-x;}
    20     if(x>9)write(x/10);
    21     putchar(x%10+'0');
    22 }
    23 struct node
    24 {
    25     int to,nex;
    26 }edge[2*(maxn*maxn+10)];
    27 int n,m,t,cnt,ans;
    28 int head[2*(maxn*maxn+10)],match[2*(maxn*maxn+10)];
    29 bool map[maxn][maxn],book[2*(maxn*maxn+10)];
    30 inline void add(int x,int y)
    31 {
    32     cnt++;
    33     edge[cnt].to=y;
    34     edge[cnt].nex=head[x];
    35     head[x]=cnt;
    36 }
    37 inline bool dfs(int u)
    38 {
    39     for(int i=head[u];i;i=edge[i].nex)
    40     {
    41         int v=edge[i].to;
    42         if(!book[v])
    43         {
    44             book[v]=1;
    45             if(!match[v]||dfs(match[v]))
    46             {
    47                 match[v]=u;
    48                 return true;
    49             }
    50         }
    51     }
    52     return false;
    53 }
    54 int main()
    55 {
    56     n=read();m=read();t=read();
    57     for(int i=1;i<=t;i++)
    58     {
    59         int x,y;
    60         x=read();y=read();
    61         map[x][y]=true;
    62     }
    63     for(int i=1;i<=n;i++)
    64         for(int j=1;j<=m;j++)
    65             if(!map[i][j])
    66             {
    67                 add(i,j);
    68             }
    69     for(int i=1;i<=n;i++)
    70     {
    71         memset(book,0,sizeof(book));
    72         if(dfs(i))ans++;
    73     }
    74     write(ans);
    75     return 0;
    76 }
    请各位大佬斧正(反正我不认识斧正是什么意思)
  • 相关阅读:
    泛型接口与NUnit初试
    异步文件IO的应用
    [Andrew McAfee]Enterprise 2.0下的大趋势
    silverlight
    扩展prototype库兼容w3c domajax for firefox
    开张了
    最长平台
    常见C/C++ XML解析器比较
    GIS地图开发
    几种常见 容器 比较和分析 hashmap, map, vector, list ...hash table
  • 原文地址:https://www.cnblogs.com/handsome-zyc/p/11242802.html
Copyright © 2020-2023  润新知