Ural1109_Conference(二分图最大匹配/匈牙利算法/网络最大流)
解题报告
二分图第一题。
题目描写叙述:
为了參加即将召开的会议,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
Memory limit: 64 MB
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 M, N, K and K pairs of representatives, finds the minimum number of needed connections.
Input
member of A and p2 is member of B.
Output
Sample
| input | output |
|---|---|
3 2 4 |
3 |
Ural1109_Conference(二分图最大匹配/匈牙利算法/网络最大流)的更多相关文章
- HDU 1045 - Fire Net - [DFS][二分图最大匹配][匈牙利算法模板][最大流求二分图最大匹配]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- HDU - 1045 Fire Net (二分图最大匹配-匈牙利算法)
(点击此处查看原题) 匈牙利算法简介 个人认为这个算法是一种贪心+暴力的算法,对于二分图的两部X和Y,记x为X部一点,y为Y部一点,我们枚举X的每个点x,如果Y部存在匹配的点y并且y没有被其他的x匹配 ...
- UESTC 919 SOUND OF DESTINY --二分图最大匹配+匈牙利算法
二分图最大匹配的匈牙利算法模板题. 由题目易知,需求二分图的最大匹配数,采取匈牙利算法,并采用邻接表来存储边,用邻接矩阵会超时,因为邻接表复杂度O(nm),而邻接矩阵最坏情况下复杂度可达O(n^3). ...
- HDU1068 (二分图最大匹配匈牙利算法)
Girls and Boys Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- poj - 3041 Asteroids (二分图最大匹配+匈牙利算法)
http://poj.org/problem?id=3041 在n*n的网格中有K颗小行星,小行星i的位置是(Ri,Ci),现在有一个强有力的武器能够用一发光速将一整行或一整列的小行星轰为灰烬,想要利 ...
- 二分图最大匹配(匈牙利算法) POJ 3041 Asteroids
题目传送门 /* 题意:每次能消灭一行或一列的障碍物,要求最少的次数. 匈牙利算法:把行和列看做两个集合,当有障碍物连接时连一条边,问题转换为最小点覆盖数==二分图最大匹配数 趣味入门:http:// ...
- 51Nod 2006 飞行员配对(二分图最大匹配)-匈牙利算法
2006 飞行员配对(二分图最大匹配) 题目来源: 网络流24题 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 第二次世界大战时期,英国皇家空军从沦陷国 ...
- poj 3894 System Engineer (二分图最大匹配--匈牙利算法)
System Engineer Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 507 Accepted: 217 Des ...
- POJ1274:The Perfect Stall(二分图最大匹配 匈牙利算法)
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17895 Accepted: 814 ...
随机推荐
- objective-III 窗口应用程序
objective-III 一.创建窗口应用程序 打开xcode->create->在iso目录下选择empty-null->创建 在打开的项目文件名上右击NEW FILE,在io ...
- CPU指令的流水线运行
指令集是CPU体系架构的重要组成部分.C语言的语法是对解决现实问题的运算和流程的方法的高度概况和抽象,其主要为算术.逻辑运算和分支控制,而指令集就是对这些抽象的详细支持,汇编仅仅只是是为了让开发者更好 ...
- 如何使用notepad++搭配MinGW配置编译C/C++
最经开始学C++了,平时学习不喜欢用IDE,一直以来都喜欢使用sublimetext写代码.所以在网上找了一下如何配置sublimetext编译C/C++.不过简单配置之后,只有输出,要想进行输出操作 ...
- Android访问网络
Android中访问网络用的是HttpClient的方式,即Apache提供的一个jar包.安卓中继承了改jar包,所以安卓adt中不需要专门import该jar,直接就可以使用. 以下是MainAc ...
- ajax 基础实例
优点:使用ajax读取数据文件,不需要刷新页面就能取出文件数据 目 录 1.0 基于ajax请求的理论支持 1.1 js 实现jquray中 ajax请求功能 基于ajax请求的理论支持 < ...
- CentOS用yum安装搭建LAMP
#1.安装Apache yum install httpd httpd-devel #启动apache /etc/init.d/httpd start #设为开机启动: chkconfig httpd ...
- C# struct 性能损失
虽然结构是值类型,但在语法上常常可以把它们当作类来处理.例如,在上面的 Dimensions 类的定义中,可以编写下面的代码:Dimensions point = new Dimensions();p ...
- Python 2.7 学习笔记 访问mysql数据库
一.基本概念 使用python操作数据库,其基本的流程如下(其实所有开发语言访问数据库的流程都是这样). 1.第一,引入相应数据库的python数据库接口模块,针对不同的数据库类型,有不同的数据库访问 ...
- 让Solr返回JSON数据
http://localhost:1985/solr/select/?q=*%3A*&version=2.2&start=0&rows=10&indent=on& ...
- 基于visual Studio2013解决C语言竞赛题之0422牛顿迭代法
题目