• hdu 2819(二分匹配)


    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2819

    思路:有矛盾关系的可以考虑二分匹配= =只交换行或者只交换列都能得到目的,如果不能,就输出-1。建图的时候应该建有向图。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<cmath>[
     6 using namespace std;
     7 #define MAXN 111
     8 int n,x;
     9 int map[MAXN][MAXN];
    10 bool mark[MAXN];
    11 int row[MAXN];
    12 int ly[MAXN];
    13 
    14 bool dfs(int u){
    15    for(int i=1;i<=n;i++){
    16       if(map[u][i]&&!mark[i]){
    17          mark[i]=true;
    18          if(ly[i]==-1||dfs(ly[i])){
    19             ly[i]=u;
    20             return true;
    21          }
    22       }
    23    }
    24    return false;
    25 }
    26 
    27 
    28 bool MaxMatch(){
    29    memset(ly,-1,sizeof(ly));
    30    for(int i=1;i<=n;i++){
    31       memset(mark,false,sizeof(mark));
    32       if(!dfs(i))return false;
    33    }
    34    return true;
    35 }
    36 
    37 
    38 int main(){
    39 //   freopen("1.txt","r",stdin);
    40    while(~scanf("%d",&n)){
    41       memset(map,0,sizeof(map));
    42       for(int i=1;i<=n;i++){
    43          for(int j=1;j<=n;j++){
    44             scanf("%d",&x);
    45             map[j][i]=x;
    46          }
    47       }
    48       if(!MaxMatch()){ puts("-1");continue; }
    49       for(int i=1;i<=n;i++){
    50          for(int j=i;j<=n;j++){
    51             if(ly[j]==i){ row[i]=j;swap(ly[j],ly[i]);break; }
    52          }
    53       }
    54       printf("%d\n",n);
    55       for(int i=1;i<=n;i++)
    56          printf("R %d %d\n",i,row[i]);
    57    }
    58    return 0;
    59 }
    View Code
  • 相关阅读:
    Springboot + Mybatis 多数据源配置
    构建微服务:Spring boot 入门篇
    IDEA SpringBoot代码修改热部署/加载
    vue-cli知识点
    vuex知识点
    正则基本知识
    多行SQL语句拼成一条数据
    软件的版本控制
    ASP.NET 表单验证实现浅析
    建造者模式
  • 原文地址:https://www.cnblogs.com/wally/p/3114786.html
Copyright © 2020-2023  润新知