解题报告

二分图第一题。

题目描写叙述:

为了參加即将召开的会议,A国派出M位代表,B国派出N位代表,(N,M<=1000)

会议召开前,选出K队代表,每对代表必须一个是A国的,一个是B国的;

要求每个代表要与还有一方的一个代表联系,除了能够直接联系,也能够电话联系,求电话联系最少

思路:

电话联系最少就要使直接联系最大,又是一一匹配关系,就是二分图的最大匹配。

以下是匈牙利算法。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 1050
#define M 1050
using namespace std;
int mmap[M][N],vis[N],pre[N],n,m,k;
int dfs(int x)
{
for(int i=1;i<=n;i++)
{
if(!vis[i]&&mmap[x][i])
{
vis[i]=1;
if(pre[i]==-1||dfs(pre[i]))
{
pre[i]=x;
return 1;
}
}
}
return 0;
}
int main()
{
int i,j,u,v;
memset(pre,-1,sizeof(pre));
memset(vis,0,sizeof(vis));
scanf("%d%d%d",&m,&n,&k);
for(i=0;i<k;i++)
{
scanf("%d%d",&u,&v);
mmap[u][v]=1;
}
int ans=0;
for(i=1;i<=m;i++)
{
memset(vis,0,sizeof(vis));
ans+=dfs(i);
}
printf("%d\n",n+m-ans);
}

二分最大匹配也能够用最大流做,当试试模板

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define inf 99999999
#define N 1050
#define M 1050
using namespace std;
int mmap[M+N][N+M],vis[N],l[N+M],n,m,k;
int bfs()
{
queue<int>Q;
Q.push(0);
memset(l,-1,sizeof(l));
l[0]=0;
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for(int i=0;i<=n+m+1;i++)
{
if(l[i]==-1&&mmap[u][i])
{
l[i]=l[u]+1;
Q.push(i);
}
}
}
if(l[n+m+1]>0)
return 1;
return 0;
}
int dfs(int x,int f)
{
int a,i;
if(x==n+m+1)
return f;
for(i=0;i<=n+m+1;i++)
{
if(mmap[x][i]&&l[i]==l[x]+1&&(a=dfs(i,min(f,mmap[x][i]))))
{
mmap[x][i]-=a;
mmap[i][x]+=a;
return a;
}
}
return 0;
}
int main()
{
int i,j,u,v;
memset(vis,0,sizeof(vis));
scanf("%d%d%d",&m,&n,&k);
for(i=1;i<=m;i++)
mmap[0][i]=1;
for(i=m+1;i<=m+n;i++)
mmap[i][m+n+1]=1;
for(i=0;i<k;i++)
{
scanf("%d%d",&u,&v);
mmap[u][v+m]=1;
}
int ans=0,a;
while(bfs())
while(a=dfs(0,inf))
ans+=a;
printf("%d\n",n+m-ans);
}

Conference

Time limit: 0.5 second

Memory limit: 64 MB
On the upcoming conference were sent M representatives of country A and N representatives of country B (M and N ≤ 1000). The representatives were
identified with 1, 2, …, M for country A and 1, 2, …, N for country B. Before the conference K pairs of representatives were chosen. Every such pair consists of one member of delegation A and one of delegation B.
If there exists a pair in which both member #i of A and member #j of B are included then #i and #j can negotiate. Everyone attending the conference was included in at least one pair. The CEO of the congress
center wants to build direct telephone connections between the rooms of the delegates, so that everyone is connected with at least one representative of the other side, and every connection is made between people that can negotiate. The CEO also wants to minimize
the amount of telephone connections. Write a program which given MNK and K pairs of representatives, finds the minimum number of needed connections.

Input

The first line of the input contains MN and K. The following K lines contain the choosen pairs in the form of two integers p1and p2, p1 is
member of A and p2 is member of B.

Output

The output should contain the minimum number of needed telephone connections.

Sample

input output
3 2 4
1 1
2 1
3 1
3 2
3
Problem Source: Bulgarian National Olympiad Day #1

Ural1109_Conference(二分图最大匹配/匈牙利算法/网络最大流)的更多相关文章

  1. HDU 1045 - Fire Net - [DFS][二分图最大匹配][匈牙利算法模板][最大流求二分图最大匹配]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 Time Limit: 2000/1000 MS (Java/Others) Mem ...

  2. HDU - 1045 Fire Net (二分图最大匹配-匈牙利算法)

    (点击此处查看原题) 匈牙利算法简介 个人认为这个算法是一种贪心+暴力的算法,对于二分图的两部X和Y,记x为X部一点,y为Y部一点,我们枚举X的每个点x,如果Y部存在匹配的点y并且y没有被其他的x匹配 ...

  3. UESTC 919 SOUND OF DESTINY --二分图最大匹配+匈牙利算法

    二分图最大匹配的匈牙利算法模板题. 由题目易知,需求二分图的最大匹配数,采取匈牙利算法,并采用邻接表来存储边,用邻接矩阵会超时,因为邻接表复杂度O(nm),而邻接矩阵最坏情况下复杂度可达O(n^3). ...

  4. HDU1068 (二分图最大匹配匈牙利算法)

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. poj - 3041 Asteroids (二分图最大匹配+匈牙利算法)

    http://poj.org/problem?id=3041 在n*n的网格中有K颗小行星,小行星i的位置是(Ri,Ci),现在有一个强有力的武器能够用一发光速将一整行或一整列的小行星轰为灰烬,想要利 ...

  6. 二分图最大匹配(匈牙利算法) POJ 3041 Asteroids

    题目传送门 /* 题意:每次能消灭一行或一列的障碍物,要求最少的次数. 匈牙利算法:把行和列看做两个集合,当有障碍物连接时连一条边,问题转换为最小点覆盖数==二分图最大匹配数 趣味入门:http:// ...

  7. 51Nod 2006 飞行员配对(二分图最大匹配)-匈牙利算法

    2006 飞行员配对(二分图最大匹配) 题目来源: 网络流24题 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 第二次世界大战时期,英国皇家空军从沦陷国 ...

  8. poj 3894 System Engineer (二分图最大匹配--匈牙利算法)

    System Engineer Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 507   Accepted: 217 Des ...

  9. POJ1274:The Perfect Stall(二分图最大匹配 匈牙利算法)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17895   Accepted: 814 ...

随机推荐

  1. PROPAGATION_REQUIRED

    PROPAGATION_REQUIRED (2009-05-13 13:26:52) 转载▼   事务传播行为种类 Spring在TransactionDefinition接口中规定了7种类型的事务传 ...

  2. BZOJ 1614: [Usaco2007 Jan]Telephone Lines架设电话线

    题目 1614: [Usaco2007 Jan]Telephone Lines架设电话线 Time Limit: 5 Sec  Memory Limit: 64 MB Description Farm ...

  3. SVN基础命令手册

    SVN版本号:1.5 及更新版本号 名词说明: WC:Working Copy 你的工作区 Versioned:受控的:受版本号控制的 SVN是什么? SVN是开源的版本号控制系统. 比CVS很多其它 ...

  4. android -- 蓝牙 bluetooth (三)搜索蓝牙

    接上篇打开蓝牙继续,来一起看下蓝牙搜索的流程,触发蓝牙搜索的条件形式上有两种,一是在蓝牙设置界面开启蓝牙会直接开始搜索,另一个是先打开蓝牙开关在进入蓝牙设置界面也会触发搜索,也可能还有其它触发方式,但 ...

  5. stm32之GPIO

    stm32有5组GPIO口,GPIOA GPIOB GPIOC GPIOD GPIOE 每个GPIO端口有: 2个配置寄存器GPIOx_CRL, GPIOx_CRH(32位): 2个数据寄存器GPIO ...

  6. BZOJ 3132: 上帝造题的七分钟( 二维BIT )

    二维树状数组... 自己YY一下再推一下应该可以搞出来... --------------------------------------------------------------------- ...

  7. python子进程模块subprocess调用shell命令

    http://www.cnblogs.com/vamei/archive/2012/09/23/2698014.html

  8. POJ 3261 可重叠的 k 次最长重复子串【后缀数组】

    这也是一道例题 给定一个字符串,求至少出现 k 次的最长重复子串,这 k 个子串可以重叠.算法分析:这题的做法和上一题差不多,也是先二分答案,然后将后缀分成若干组.不同的是,这里要判断的是有没有一个组 ...

  9. 互联网创业十问?good(快速迭代、把握核心用户应对抄袭,不需要把商业模式考虑完备,4种失败的信号,失败者没资格说趁着年轻...)

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:曹政链接:https://www.zhihu.com/question/20264499/answer/28168079来源: ...

  10. docker学习笔记17:Dockerfile 指令 ONBUILD介绍

    ONBUILD指令可以为镜像添加触发器.其参数是任意一个Dockerfile 指令. 当我们在一个Dockerfile文件中加上ONBUILD指令,该指令对利用该Dockerfile构建镜像(比如为A ...